Update main.py
Browse files
main.py
CHANGED
|
@@ -1,45 +1,87 @@
|
|
| 1 |
-
import
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from tavily import TavilyClient
|
| 5 |
from datetime import date
|
| 6 |
-
from
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
|
|
| 9 |
|
| 10 |
class PromptRequest(BaseModel):
|
| 11 |
prompt: str
|
| 12 |
temperature: float = 0.5
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.get("/")
|
| 15 |
def health():
|
| 16 |
return {"ok": True}
|
| 17 |
|
| 18 |
-
today_date = date.today()
|
| 19 |
-
|
| 20 |
-
def search_tool(query:str):
|
| 21 |
-
api_key = os.environ.get("TAVILY_API_KEY")
|
| 22 |
-
client = TavilyClient(api_key)
|
| 23 |
-
response = client.search(query=query, include_answer="advanced", search_depth="advanced")
|
| 24 |
-
return response
|
| 25 |
-
|
| 26 |
@app.post("/qwen2.5-coder:7b")
|
| 27 |
async def generate_response(request: PromptRequest):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
if "Search "
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
print(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
|
|
|
| 4 |
from datetime import date
|
| 5 |
+
from ollama import Client
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
+
today_date = date.today()
|
| 9 |
|
| 10 |
class PromptRequest(BaseModel):
|
| 11 |
prompt: str
|
| 12 |
temperature: float = 0.5
|
| 13 |
|
| 14 |
+
TOOL_PROMPT = f"""You are an autonomous AI Agent with real-time internet access.
|
| 15 |
+
Current Date: {today_date}
|
| 16 |
+
|
| 17 |
+
You have access to one tool:
|
| 18 |
+
- Search [query] : searches the internet and returns real-time results
|
| 19 |
+
|
| 20 |
+
RULES:
|
| 21 |
+
- If the user query needs current/real-time/recent information, respond with ONLY: Search [your search query]
|
| 22 |
+
- If you can answer from your own knowledge, answer directly
|
| 23 |
+
- Do NOT mix search command with other text
|
| 24 |
+
- After getting search results, answer the user query using those results
|
| 25 |
+
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
def search_tool(query: str) -> str:
|
| 29 |
+
try:
|
| 30 |
+
r = requests.get(
|
| 31 |
+
"https://s.jina.ai/",
|
| 32 |
+
params={"q": query},
|
| 33 |
+
headers={"Accept": "application/json", "X-Retain-Images": "none"},
|
| 34 |
+
timeout=10
|
| 35 |
+
)
|
| 36 |
+
data = r.json().get("data", [])[:5]
|
| 37 |
+
results = []
|
| 38 |
+
for item in data:
|
| 39 |
+
title = item.get("title", "")
|
| 40 |
+
desc = item.get("description", "") or item.get("content", "")[:300]
|
| 41 |
+
url = item.get("url", "")
|
| 42 |
+
results.append(f"- {title}\n {desc}\n Source: {url}")
|
| 43 |
+
return "\n\n".join(results) if results else "No results found."
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Search failed: {str(e)}"
|
| 46 |
+
|
| 47 |
+
def run_llm(messages: list, temperature: float) -> str:
|
| 48 |
+
client = Client(host="http://localhost:11434")
|
| 49 |
+
response = client.chat(
|
| 50 |
+
model="qwen2.5-coder:7b",
|
| 51 |
+
messages=messages,
|
| 52 |
+
options={"temperature": temperature}
|
| 53 |
+
)
|
| 54 |
+
return response["message"]["content"].strip()
|
| 55 |
+
|
| 56 |
@app.get("/")
|
| 57 |
def health():
|
| 58 |
return {"ok": True}
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
@app.post("/qwen2.5-coder:7b")
|
| 61 |
async def generate_response(request: PromptRequest):
|
| 62 |
+
# Step 1: ask LLM if it needs to search
|
| 63 |
+
messages = [
|
| 64 |
+
{"role": "system", "content": TOOL_PROMPT},
|
| 65 |
+
{"role": "user", "content": request.prompt}
|
| 66 |
+
]
|
| 67 |
+
response = run_llm(messages, request.temperature)
|
| 68 |
+
print(f"[LLM RAW]: {response}")
|
| 69 |
+
|
| 70 |
+
# Step 2: if LLM wants to search
|
| 71 |
+
if response.strip().startswith("Search "):
|
| 72 |
+
query = response.strip().removeprefix("Search ").strip("[]").strip()
|
| 73 |
+
print(f"[SEARCH QUERY]: {query}")
|
| 74 |
+
search_results = search_tool(query)
|
| 75 |
+
print(f"[SEARCH RESULTS]: {search_results[:200]}...")
|
| 76 |
+
|
| 77 |
+
# Step 3: feed results back to LLM
|
| 78 |
+
messages.append({"role": "assistant", "content": response})
|
| 79 |
+
messages.append({
|
| 80 |
+
"role": "user",
|
| 81 |
+
"content": f"Search Results:\n{search_results}\n\nNow answer the original question: {request.prompt}"
|
| 82 |
+
})
|
| 83 |
+
final_response = run_llm(messages, request.temperature)
|
| 84 |
+
print(f"[FINAL]: {final_response}")
|
| 85 |
+
return {"response": final_response, "searched": True, "query": query}
|
| 86 |
|
| 87 |
+
return {"response": response, "searched": False}
|