File size: 2,516 Bytes
b284b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e562f9
b284b20
 
3e562f9
b284b20
 
 
 
 
 
9894544
b284b20
 
9894544
b284b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2996c95
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os 
from fastapi import FastAPI 
from pydantic import BaseModel 
from tavily import TavilyClient 
from datetime import date 
from langchain_ollama.llms import OllamaLLM 
from fastapi.responses import StreamingResponse 

app = FastAPI() 

class PromptRequest(BaseModel): 
    prompt: str 
    temperature: float = 0.5 
    

@app.get("/") 
def health(): 
    return {"ok": True} 
    

today_date = date.today() 

def search_tool(query:str): 
    api_key= os.environ.get("TAVILY_API_KEY") 
    client = TavilyClient(api_key) 
    response = client.search( query=query, include_answer="advanced", search_depth="advanced" ) 
    return response 
    
@app.post("/qwen2.5:7b") 
async def generate_response(request: PromptRequest): 
    llm = OllamaLLM( 
        model="qwen2.5:7b", 
        temperature=request.temperature, 
        base_url="http://localhost:11436" 
    ) 
    response = llm.invoke(request.prompt) 
    return {"response": response} 

@app.post("/llama3.2:1b") 
async def qwen_generate_response(request:PromptRequest): 
    llm = OllamaLLM( 
        model="llama3.2:1b", 
        temperature=request.temperature, 
        base_url="http://localhost:11435" 
    ) 
    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] - Use Case: Use ONLY when you lack specific facts, need the latest 2026 data, or your training data is outdated. STRICT OUTPUT RULES: 1. To use the tool: Your entire response must start with the word "Search" followed by your query. Example: Search Who is the current Prime Minister of India? 2. To answer normally: If you already have the information, provide a direct answer. 3. DO NOT use the word "Search" at the beginning of your response unless you are calling the tool. 4. DO NOT use brackets or quotes in the tool call. """ 
    response = llm.invoke(f'{tool_prompt}, User-Query:-{request.prompt}') 
    print("RESPONSE") 
    print(response) 
    if "Search " in response: 
        new_query = response.removeprefix("Search ") 
        print("NEW_QUERY") 
        print(new_query) 
        search_response = search_tool(query=new_query) 
        print("SEARCH_RESPONSE") 
        print(search_response) 
        new_response = llm.invoke(f"Extra_information:- {search_response} User-Query:- {request.prompt}") 
        return {"response": new_response} 
    else: 
        return {"response": response}