Upload main.py with huggingface_hub
Browse files
main.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from tavily import TavilyClient
|
| 5 |
+
from datetime import date
|
| 6 |
+
from langchain_ollama.llms import OllamaLLM
|
| 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("/gemma4:e2b")
|
| 27 |
+
async def generate_response(request: PromptRequest):
|
| 28 |
+
llm = OllamaLLM(
|
| 29 |
+
model="gemma4:e2b",
|
| 30 |
+
temperature=request.temperature,
|
| 31 |
+
base_url="http://localhost:11436"
|
| 32 |
+
)
|
| 33 |
+
tool_prompt = f"System Role: You are an autonomous AI Agent with real-time internet access. Current Date: {today_date} TOOL_DEFINITION: - Name: Search_tool - Activation Command: Search [Your Query Here]"
|
| 34 |
+
response = llm.invoke(f'{tool_prompt}, User-Query:-{request.prompt}')
|
| 35 |
+
|
| 36 |
+
if "Search " in response:
|
| 37 |
+
new_query = response.removeprefix("Search ")
|
| 38 |
+
search_response = search_tool(query=new_query)
|
| 39 |
+
new_response = llm.invoke(f"Extra_information:- {search_response} User-Query:- {request.prompt}")
|
| 40 |
+
return {"response": new_response}
|
| 41 |
+
else:
|
| 42 |
+
return {"response": response}
|
| 43 |
+
|
| 44 |
+
@app.post("/qwen3.5:2b")
|
| 45 |
+
async def qwen_generate_response(request:PromptRequest):
|
| 46 |
+
llm = OllamaLLM(
|
| 47 |
+
model="qwen3.5:2b",
|
| 48 |
+
temperature=request.temperature,
|
| 49 |
+
base_url="http://localhost:11435"
|
| 50 |
+
)
|
| 51 |
+
tool_prompt = f"System Role: You are an autonomous AI Agent with real-time internet access. Current Date: {today_date} TOOL_DEFINITION: - Name: Search_tool - Activation Command: Search [Your Query Here]"
|
| 52 |
+
response = llm.invoke(f'{tool_prompt}, User-Query:-{request.prompt}')
|
| 53 |
+
|
| 54 |
+
if "Search " in response:
|
| 55 |
+
new_query = response.removeprefix("Search ")
|
| 56 |
+
search_response = search_tool(query=new_query)
|
| 57 |
+
new_response = llm.invoke(f"Extra_information:- {search_response} User-Query:- {request.prompt}")
|
| 58 |
+
return {"response": new_response}
|
| 59 |
+
else:
|
| 60 |
+
return {"response": response}
|