Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,10 @@ from llama_index.llms.gemini import Gemini
|
|
| 11 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 12 |
from llama_index.readers.web import FireCrawlWebReader
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
dotenv.load_dotenv()
|
| 16 |
|
|
@@ -125,6 +129,28 @@ async def query(request: QueryRequest):
|
|
| 125 |
|
| 126 |
return {"response": response.response}
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
@app.get("/chat-history/")
|
| 129 |
async def get_chat_history():
|
| 130 |
return {"chat_history": state['chat_history']}
|
|
|
|
| 11 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 12 |
from llama_index.readers.web import FireCrawlWebReader
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
+
from llama_index.core.agent import ReActAgent
|
| 15 |
+
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
| 16 |
+
from llama_index.core.llms import ChatMessage
|
| 17 |
+
|
| 18 |
|
| 19 |
dotenv.load_dotenv()
|
| 20 |
|
|
|
|
| 129 |
|
| 130 |
return {"response": response.response}
|
| 131 |
|
| 132 |
+
|
| 133 |
+
@app.post("/agent-search/")
|
| 134 |
+
async def agent_search(request: QueryRequest):
|
| 135 |
+
code_tools=DuckDuckGoSearchToolSpec()
|
| 136 |
+
tool = code_tools.to_tool_list()
|
| 137 |
+
llm = Gemini(api_key=os.environ["GOOGLE_API_KEY"], model="models/gemini-1.5-pro")
|
| 138 |
+
prefix_messages = [
|
| 139 |
+
ChatMessage(
|
| 140 |
+
role="system",
|
| 141 |
+
content=(
|
| 142 |
+
"You are now a integration agent, and what ever you are requested, you will try to execute utilizing your toools."
|
| 143 |
+
),
|
| 144 |
+
)
|
| 145 |
+
]
|
| 146 |
+
agent = ReActAgent.from_tools(tool, llm=llm, verbose=True)
|
| 147 |
+
response = agent.chat(request.query)
|
| 148 |
+
state['chat_history'].append(("User", request.query))
|
| 149 |
+
state['chat_history'].append(("Assistant", str(response.response)))
|
| 150 |
+
|
| 151 |
+
return {"response": response.response}
|
| 152 |
+
|
| 153 |
+
|
| 154 |
@app.get("/chat-history/")
|
| 155 |
async def get_chat_history():
|
| 156 |
return {"chat_history": state['chat_history']}
|