Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,16 @@ 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 |
|
|
@@ -66,6 +76,48 @@ def embed_setup():
|
|
| 66 |
Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 67 |
Settings.llm = Gemini(temperature=0.1, model_name="models/gemini-1.5-pro")
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
def qdrant_setup():
|
| 70 |
client = qdrant_client.QdrantClient(
|
| 71 |
os.getenv("QDRANT_URL"),
|
|
@@ -174,6 +226,8 @@ async def agent_search(request: QueryRequest):
|
|
| 174 |
async def get_chat_history():
|
| 175 |
return {"chat_history": state['chat_history']}
|
| 176 |
|
|
|
|
|
|
|
| 177 |
@app.post("/clear-chat/")
|
| 178 |
async def clear_chat():
|
| 179 |
state['chat_history'] = []
|
|
|
|
| 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 |
+
from composio_llamaindex import ComposioToolSet, App, Action
|
| 18 |
+
from llama_index.core.agent import FunctionCallingAgentWorker
|
| 19 |
+
from llama_index.core.llms import ChatMessage
|
| 20 |
+
from llama_index.llms.openai import OpenAI
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
|
| 23 |
+
class AgentInput(BaseModel):
|
| 24 |
+
sheet_id: str
|
| 25 |
+
api_key: str
|
| 26 |
+
query: str
|
| 27 |
|
| 28 |
|
| 29 |
|
|
|
|
| 76 |
Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 77 |
Settings.llm = Gemini(temperature=0.1, model_name="models/gemini-1.5-pro")
|
| 78 |
|
| 79 |
+
@app.post("/sheets_query/")
|
| 80 |
+
async def process_query(input_data: AgentInput):
|
| 81 |
+
# Load environment variables
|
| 82 |
+
load_dotenv()
|
| 83 |
+
|
| 84 |
+
# Set up OpenAI LLM
|
| 85 |
+
os.environ["OPENAI_API_KEY"] = input_data.api_key
|
| 86 |
+
llm = OpenAI(model='gpt-4o')
|
| 87 |
+
|
| 88 |
+
# Set up ComposioToolSet
|
| 89 |
+
composio_toolset = ComposioToolSet(api_key=os.getenv("COMPOSIO_API_KEY"), output_dir=Path("./plots/"))
|
| 90 |
+
tools = composio_toolset.get_tools(apps=[App.GOOGLESHEETS])
|
| 91 |
+
|
| 92 |
+
# Define prefix messages
|
| 93 |
+
prefix_messages = [
|
| 94 |
+
ChatMessage(
|
| 95 |
+
role="system",
|
| 96 |
+
content=(
|
| 97 |
+
"You are an AI assistant who is an expert at Google Sheets. "
|
| 98 |
+
"Use Google Sheets Tool and perform the necessary operations based on query"
|
| 99 |
+
)
|
| 100 |
+
)
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
# Create agent
|
| 104 |
+
agent = FunctionCallingAgentWorker(
|
| 105 |
+
tools=tools,
|
| 106 |
+
llm=llm,
|
| 107 |
+
prefix_messages=prefix_messages,
|
| 108 |
+
max_function_calls=10,
|
| 109 |
+
allow_parallel_tool_calls=False,
|
| 110 |
+
verbose=True
|
| 111 |
+
).as_agent()
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
# Process the query
|
| 115 |
+
response = agent.chat(f"This is the Google Sheet ID: {input_data.sheet_id}. {input_data.query}")
|
| 116 |
+
return {"response": response}
|
| 117 |
+
except Exception as e:
|
| 118 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 119 |
+
|
| 120 |
+
|
| 121 |
def qdrant_setup():
|
| 122 |
client = qdrant_client.QdrantClient(
|
| 123 |
os.getenv("QDRANT_URL"),
|
|
|
|
| 226 |
async def get_chat_history():
|
| 227 |
return {"chat_history": state['chat_history']}
|
| 228 |
|
| 229 |
+
|
| 230 |
+
|
| 231 |
@app.post("/clear-chat/")
|
| 232 |
async def clear_chat():
|
| 233 |
state['chat_history'] = []
|