Spaces:
Build error
Build error
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 | |