File size: 1,282 Bytes
bc44283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import openai
import json
import os

# Load API key from environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")

# Dummy articles for demonstration (replace with real data as needed)
articles = [
    {"title": "Dealing with Depression", "link": "https://example.com/depression"},
    {"title": "Managing Anxiety", "link": "https://example.com/anxiety"},
    {"title": "Overcoming Stress", "link": "https://example.com/stress"},
]

def retrieve_articles(prompt):
    # Dummy retrieval logic (returns first few articles)
    return articles[:3]

def generate_response(prompt):
    retrieved_articles = retrieve_articles(prompt)
    
    context = "\n".join([f"Title: {article['title']}\nContent: {article['link']}" for article in retrieved_articles])
    
    response = openai.Completion.create(
        engine="text-davinci-003",  # or another OpenAI model
        prompt=f"{context}\n\nUser: {prompt}\nChatbot:",
        max_tokens=150
    )
    
    combined_response = "Here are some resources that might help:\n\n"
    for article in retrieved_articles:
        combined_response += f"{article['title']}\n{article['link']}\n\n"
    
    combined_response += f"\nAdditionally, here's some advice:\n{response.choices[0].text.strip()}"
    
    return combined_response