Delete table_maker.py
Browse files- table_maker.py +0 -106
table_maker.py
DELETED
|
@@ -1,106 +0,0 @@
|
|
| 1 |
-
from pydantic_graph import BaseNode, End, GraphRunContext, Graph
|
| 2 |
-
from pydantic_ai import Agent
|
| 3 |
-
from pydantic_ai.common_tools.tavily import tavily_search_tool
|
| 4 |
-
from dataclasses import dataclass
|
| 5 |
-
from pydantic import Field, BaseModel
|
| 6 |
-
from typing import List, Dict, Optional, Any
|
| 7 |
-
from pydantic_ai.models.gemini import GeminiModel
|
| 8 |
-
from pydantic_ai.providers.google_gla import GoogleGLAProvider
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
-
import os
|
| 11 |
-
from tavily import TavilyClient
|
| 12 |
-
from IPython.display import Image, display
|
| 13 |
-
import requests
|
| 14 |
-
|
| 15 |
-
load_dotenv()
|
| 16 |
-
google_api_key=os.getenv('google_api_key')
|
| 17 |
-
tavily_key=os.getenv('tavily_key')
|
| 18 |
-
tavily_client = TavilyClient(api_key=tavily_key)
|
| 19 |
-
llm=GeminiModel('gemini-2.0-flash', provider=GoogleGLAProvider(api_key=google_api_key))
|
| 20 |
-
|
| 21 |
-
@dataclass
|
| 22 |
-
class State:
|
| 23 |
-
query:str
|
| 24 |
-
research:List[str]
|
| 25 |
-
table:dict
|
| 26 |
-
preliminary_research:str
|
| 27 |
-
research_plan:List[str]
|
| 28 |
-
|
| 29 |
-
#define the table row and table schema
|
| 30 |
-
class Table_row(BaseModel):
|
| 31 |
-
data: List[str] = Field(description='the data of the row')
|
| 32 |
-
class Table(BaseModel):
|
| 33 |
-
rows: List[Table_row] = Field(description='the rows of the table')
|
| 34 |
-
columns: List[str] = Field(description='the columns of the table')
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
class table_maker_node(BaseNode[State]):
|
| 39 |
-
async def run(self, ctx: GraphRunContext[State])->End:
|
| 40 |
-
table_agent=Agent(llm, result_type=Table, system_prompt="generate a detailed table in a dictionary format based on the research and the query")
|
| 41 |
-
table=await table_agent.run(f'query:{ctx.state.query}, research:{ctx.state.research}')
|
| 42 |
-
ctx.state.table={'data':[row.data for row in table.data.rows], 'columns':table.data.columns}
|
| 43 |
-
return End(ctx.state.table)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
class data_research_node(BaseNode[State]):
|
| 47 |
-
async def run(self, ctx: GraphRunContext[State])->table_maker_node:
|
| 48 |
-
for i in ctx.state.research_plan:
|
| 49 |
-
response = tavily_client.search(i.search_query)
|
| 50 |
-
|
| 51 |
-
for i in response.get('results'):
|
| 52 |
-
if i.get('score')>0.50:
|
| 53 |
-
ctx.state.research.append(i.get('content'))
|
| 54 |
-
return table_maker_node()
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
class search_query(BaseModel):
|
| 59 |
-
search_query: str = Field(description='the detailed web search query for the research')
|
| 60 |
-
|
| 61 |
-
class Research_plan(BaseModel):
|
| 62 |
-
search_queries: List[search_query] = Field(description='the detailed web search queries for the research')
|
| 63 |
-
|
| 64 |
-
research_plan_agent=Agent(llm, result_type=Research_plan, system_prompt='generate a detailed research plan breaking down the research into smaller parts based on the query and the preliminary search')
|
| 65 |
-
|
| 66 |
-
class Research_plan_node(BaseNode[State]):
|
| 67 |
-
async def run(self, ctx: GraphRunContext[State])->data_research_node:
|
| 68 |
-
|
| 69 |
-
prompt=(f'query:{ctx.state.query}, preliminary_search:{ctx.state.preliminary_research}')
|
| 70 |
-
result=await research_plan_agent.run(prompt)
|
| 71 |
-
ctx.state.research_plan=result.data.search_queries
|
| 72 |
-
return data_research_node()
|
| 73 |
-
|
| 74 |
-
class preliminary_search_node(BaseNode[State]):
|
| 75 |
-
async def run(self, ctx: GraphRunContext[State]) -> Research_plan_node:
|
| 76 |
-
search_agent=Agent(llm, tools=[tavily_search_tool(tavily_key)], system_prompt="do a websearch based on the query")
|
| 77 |
-
prompt = (' Do a preliminary search to get a global idea of the subject that the user wants to do reseach on as well as the necessary informations to do a search on.\n'
|
| 78 |
-
f'The subject is based on the query: {ctx.state.query}, return the results of the search.')
|
| 79 |
-
result=await search_agent.run(prompt)
|
| 80 |
-
ctx.state.preliminary_research=result.data
|
| 81 |
-
return Research_plan_node()
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
class table_maker_engine:
|
| 85 |
-
def __init__(self):
|
| 86 |
-
self.graph=Graph(nodes=[preliminary_search_node, Research_plan_node, data_research_node, table_maker_node])
|
| 87 |
-
self.state=State(query='', research=[], table={}, preliminary_research='', research_plan=[])
|
| 88 |
-
|
| 89 |
-
async def chat(self,query:str):
|
| 90 |
-
"""Chat with the table maker engine,
|
| 91 |
-
Args:
|
| 92 |
-
query (str): The query to search for
|
| 93 |
-
Returns:
|
| 94 |
-
str: The response from the table maker engine
|
| 95 |
-
"""
|
| 96 |
-
self.state.query=query
|
| 97 |
-
response=await self.graph.run(preliminary_search_node(),state=self.state)
|
| 98 |
-
return response.output
|
| 99 |
-
|
| 100 |
-
def display_graph(self):
|
| 101 |
-
"""Display the graph of the table maker engine
|
| 102 |
-
Returns:
|
| 103 |
-
Image: The image of the graph
|
| 104 |
-
"""
|
| 105 |
-
image=self.graph.mermaid_image()
|
| 106 |
-
return display(Image(image))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|