Pranjali29 commited on
Commit
344b321
·
verified ·
1 Parent(s): b2170db

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +89 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -----------------------------
2
+ # 1. Imports
3
+ # -----------------------------
4
+ from langchain.agents import create_agent
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain_core.tools import tool
7
+ from tavily import TavilyClient
8
+ import os
9
+
10
+ # -----------------------------
11
+ # 2. API Keys (IMPORTANT)
12
+ # -----------------------------
13
+ OPENROUTER_API_KEY = "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"
14
+ TAVILY_API_KEY = "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"
15
+
16
+ # -----------------------------
17
+ # 3. LLM Setup (OpenRouter)
18
+ # -----------------------------
19
+ llm = ChatOpenAI(
20
+ model="meta-llama/llama-3-8b-instruct",
21
+ openai_api_base="https://openrouter.ai/api/v1",
22
+ openai_api_key=OPENROUTER_API_KEY,
23
+ temperature=0.3
24
+ )
25
+
26
+ # -----------------------------
27
+ # 4. Tools
28
+ # -----------------------------
29
+ tavily = TavilyClient(api_key=TAVILY_API_KEY)
30
+
31
+ @tool
32
+ def search_tool(query: str) -> str:
33
+ """Search the web for latest info."""
34
+ results = tavily.search(query=query, max_results=3)
35
+ return str(results)
36
+
37
+ @tool
38
+ def summarize_tool(text: str) -> str:
39
+ """Summarize text into insights."""
40
+ prompt = f"""
41
+ Summarize into:
42
+ - Key Points
43
+ - Risks
44
+ - Opportunities
45
+
46
+ {text}
47
+ """
48
+ return llm.invoke(prompt).content
49
+
50
+ # -----------------------------
51
+ # 5. Agent
52
+ # -----------------------------
53
+ agent = create_agent(
54
+ model=llm,
55
+ tools=[search_tool, summarize_tool],
56
+ system_prompt="""
57
+ You are an intelligent research assistant.
58
+
59
+ Steps:
60
+ 1. Search information
61
+ 2. Summarize it
62
+ 3. Give final structured answer
63
+ """
64
+ )
65
+
66
+ # -----------------------------
67
+ # 6. Function for UI
68
+ # -----------------------------
69
+ def chat(query):
70
+ response = agent.invoke({
71
+ "messages": [
72
+ {"role": "user", "content": query}
73
+ ]
74
+ })
75
+ return response["messages"][-1].content
76
+
77
+
78
+ import gradio as gr
79
+
80
+ interface = gr.Interface(
81
+ fn=chat,
82
+ inputs=gr.Textbox(lines=2, placeholder="Ask something..."),
83
+ outputs="text",
84
+ title="AI Research Assistant",
85
+ description="Search + Summarize AI Agent"
86
+ )
87
+
88
+ if __name__ == "__main__":
89
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ langchain
2
+ langchain-openai
3
+ tavily-python
4
+ gradio