import requests
# Desearch API Configuration
API_KEY = "dt_$YOUR_API_KEY"
HEADERS = {
"Authorization": API_KEY,
"Content-Type": "application/json"
}
# Function to Fetch AI Search Insights
def ai_search(prompt):
url = "https://apis.datura.ai/desearch/ai/search"
payload = {
"model": "NOVA",
"prompt": prompt,
"streaming": False
}
response = requests.post(url, json=payload, headers=HEADERS)
return response.json()
# Function to Fetch X (Twitter) Data for Chatbot Context
def X_search(query):
url = "https://apis.datura.ai/desearch/X/search"
payload = {
"query": query,
"date_filter": "PAST_24_HOURS",
"tools": ["Twitter Search"]
}
response = requests.post(url, json=payload, headers=HEADERS)
return response.json()
# Function to Enrich Responses with Web Search
def web_search(query):
url = "https://apis.datura.ai/desearch/web/search"
payload = {
"query": query,
"results_limit": 5
}
response = requests.post(url, json=payload, headers=HEADERS)
return response.json()
# Function to Generate Chatbot Response
def chatbot_response(user_query):
print("🤖 Thinking... Fetching AI insights...")
ai_response = ai_search(user_query)
print("🔍 Searching X (Twitter) for latest discussions...")
X_response = X_search(user_query)
print("🌍 Fetching Web insights for better context...")
web_response = web_search(user_query)
# Extract Top Insights
ai_text = ai_response.get("summary", "No AI response found.")
X_texts = [tweet.get("text", "") for tweet in X_response.get("results", [])][:3]
web_texts = [result.get("title", "") for result in web_response.get("results", [])][:3]
# Final Chatbot Response
chatbot_reply = f"""
🤖 **AI Chatbot Response**
🔹 **AI Insight:** {ai_text}
🐦 **Latest X Discussions:**
- {X_texts[0] if X_texts else 'No relevant tweets found'}
- {X_texts[1] if len(X_texts) > 1 else ''}
- {X_texts[2] if len(X_texts) > 2 else ''}
🌍 **Web Insights:**
- {web_texts[0] if web_texts else 'No relevant web articles found'}
- {web_texts[1] if len(web_texts) > 1 else ''}
- {web_texts[2] if len(web_texts) > 2 else ''}
"""
return chatbot_reply
# Example Usage
if __name__ == "__main__":
user_input = input("Ask me anything: ")
response = chatbot_response(user_input)
print(response)