agentharbor commited on
Commit
8120752
·
verified ·
1 Parent(s): 8c87e78

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ['COHERE_API_KEY'] = ""
3
+ # Create the Cohere chat model
4
+ from langchain_cohere.chat_models import ChatCohere
5
+ chat = ChatCohere(model="command-r-plus", temperature=0.3)
6
+ from langchain_community.tools.tavily_search import TavilySearchResults
7
+
8
+ os.environ['TAVILY_API_KEY'] = ""
9
+
10
+ internet_search = TavilySearchResults()
11
+ internet_search.name = "internet_search"
12
+ internet_search.description = "Returns a list of relevant document snippets for a textual query retrieved from the internet."
13
+
14
+
15
+ from langchain_core.pydantic_v1 import BaseModel, Field
16
+ class TavilySearchInput(BaseModel):
17
+ query: str = Field(description="Query to search the internet with")
18
+ internet_search.args_schema = TavilySearchInput
19
+ from langchain.agents import Tool
20
+ from langchain_experimental.utilities import PythonREPL
21
+
22
+ python_repl = PythonREPL()
23
+ repl_tool = Tool(
24
+ name="python_repl",
25
+ description="Executes python code and returns the result. The code runs in a static sandbox without interactive mode, so print output or save output to a file.",
26
+ func=python_repl.run,
27
+ )
28
+ repl_tool.name = "python_interpreter"
29
+
30
+ # from langchain_core.pydantic_v1 import BaseModel, Field
31
+ class ToolInput(BaseModel):
32
+ code: str = Field(description="Python code to execute.")
33
+ repl_tool.args_schema = ToolInput
34
+ from langchain.agents import AgentExecutor
35
+ from langchain_cohere.react_multi_hop.agent import create_cohere_react_agent
36
+ from langchain_core.prompts import ChatPromptTemplate
37
+ # Create the prompt
38
+ prompt = ChatPromptTemplate.from_template("{input}")
39
+
40
+
41
+ # Create the ReAct agent
42
+ agent = create_cohere_react_agent(
43
+ llm=chat,
44
+ tools=[internet_search, repl_tool],
45
+ prompt=prompt,
46
+ )
47
+ agent_executor = AgentExecutor(agent=agent, tools=[internet_search, repl_tool], verbose=True)
48
+ from typing import List, Mapping, Any
49
+ from langchain_cohere.common import CohereCitation
50
+ def process_data(problem):
51
+ output = 'Gemini agent rewriting your query \n\n'
52
+ yield output
53
+ #rewrite = get_completion(f"Rewrite the user question: {problem} ")
54
+ #output += f"Here is your rewritten query: {rewrite} \n\n"
55
+ #yield output
56
+ output += f"Cohere agent gathering the data from public sources and doing analysis \n\n"
57
+ yield output
58
+ coh_output = agent_executor.invoke({
59
+ "input": f"{problem}"})
60
+ print ("Output is",coh_output['output'])
61
+ output += f"Final Output: \n\n"+coh_output['output']+"\n\n"
62
+ yield output
63
+ citations = coh_output['citations']
64
+ # Assuming 'citi' is a list of CohereCitation objects
65
+ urls = []
66
+ for item in citations:
67
+ if isinstance(item, CohereCitation) and item.documents:
68
+ for doc in item.documents:
69
+ if 'url' in doc:
70
+ urls.append(doc['url'])
71
+ final_urls = list(set(urls))
72
+ output += f"Citations: \n\n" + '\n'.join(final_urls)
73
+ yield output
74
+ #add_conversation(problem,output)
75
+ return output