Rename agent to agent/agent.py
Browse files- agent +0 -0
- agent/agent.py +87 -0
agent
DELETED
|
File without changes
|
agent/agent.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
|
| 4 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 5 |
+
from langgraph.prebuilt import tools_condition
|
| 6 |
+
from langgraph.prebuilt import ToolNode
|
| 7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
+
from langchain_groq import ChatGroq
|
| 9 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 10 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 11 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 12 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 13 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 14 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 15 |
+
|
| 16 |
+
from langchain_core.tools import tool
|
| 17 |
+
from langchain.tools.retriever import create_retriever_tool
|
| 18 |
+
from supabase.client import Client, create_client
|
| 19 |
+
|
| 20 |
+
from tools.basic_calculator import add, count_substring, divide, modulus, multiply, power, square_root, subtract
|
| 21 |
+
from tools.code_interpreter import execute_code_multilang
|
| 22 |
+
from tools.document_processing import save_and_read_file,download_file_from_url, extract_text_from_image, analyze_csv_file, analyze_excel_file
|
| 23 |
+
from tools.image_processing import analyze_image, transform_image, draw_on_image, generate_simple_image, combine_images
|
| 24 |
+
from tools.web_search import arxiv_search, similar_question_search, wiki_search, web_search
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
load_dotenv() # load environment variables
|
| 28 |
+
|
| 29 |
+
# load the system prompt from the file
|
| 30 |
+
with open("prompts/system_prompt.txt", "r", encoding="utf-8") as f:
|
| 31 |
+
system_prompt = f.read()
|
| 32 |
+
print(system_prompt)
|
| 33 |
+
|
| 34 |
+
# System message
|
| 35 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 36 |
+
|
| 37 |
+
# build a retriever
|
| 38 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") # set the model to generate embeddings; dim=768
|
| 39 |
+
supabase:Client = create_client(os.environ.get("SUPABASE_URL"), os.environ.get("SUPABASE_SERVICE_KEY"))
|
| 40 |
+
vector_store = SupabaseVectorStore(client=supabase, embedding= embeddings, table_name="documents", query_name="match_documents_langchain")
|
| 41 |
+
create_retriever_tool = create_retriever_tool(retriever=vector_store.as_retriever(), name="Question Retriever", description="A tool to retrieve similar questions from a vector store.")
|
| 42 |
+
|
| 43 |
+
tools = [web_search, wiki_search, similar_question_search, arxiv_search, multiply, add, subtract, divide, modulus, power, square_root, count_substring, save_and_read_file, download_file_from_url, extract_text_from_image, analyze_csv_file, analyze_excel_file, execute_code_multilang, analyze_image, transform_image, draw_on_image, generate_simple_image, combine_images]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Build the agent graph
|
| 47 |
+
def build_graph(provider: str = "huggingface-qwen"):
|
| 48 |
+
"""Build the graph"""
|
| 49 |
+
# Load environment variables from .env file
|
| 50 |
+
if provider == "google": # Google Gemini
|
| 51 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 52 |
+
elif provider == "groq": # Groq https://console.groq.com/docs/models
|
| 53 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0) # optional : qwen-qwq-32b gemma2-9b-it
|
| 54 |
+
elif provider == "huggingface-qwen":
|
| 55 |
+
llm = ChatHuggingFace(llm=HuggingFaceEndpoint(repo_id = "Qwen/Qwen2.5-Coder-32B-Instruct"))
|
| 56 |
+
elif provider == "huggingface-llama":
|
| 57 |
+
llm = ChatHuggingFace(llm=HuggingFaceEndpoint(repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0", task="text-generation", max_new_tokens=1024, do_sample=False, repetition_penalty=1.03, temperature=0), verbose=True)
|
| 58 |
+
else:
|
| 59 |
+
raise ValueError("Invalid provider. Choose 'google', 'groq', 'huggingface-qwen' or 'huggingface-llama'.")
|
| 60 |
+
|
| 61 |
+
llm_with_tools = llm.bind_tools(tools) # Bind tools to LLM
|
| 62 |
+
|
| 63 |
+
# Node
|
| 64 |
+
def assistant(state: MessagesState):
|
| 65 |
+
"""Assistant node"""
|
| 66 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 67 |
+
|
| 68 |
+
def retriever(state: MessagesState):
|
| 69 |
+
"""Retriever node"""
|
| 70 |
+
similar_question = vector_store.similarity_search(state["messages"][0].content)
|
| 71 |
+
example_msg = HumanMessage(content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}")
|
| 72 |
+
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
| 73 |
+
|
| 74 |
+
# create nodes - decision points
|
| 75 |
+
builder = StateGraph(MessagesState)
|
| 76 |
+
builder.add_node("retriever", retriever)
|
| 77 |
+
builder.add_node("assistant", assistant)
|
| 78 |
+
builder.add_node("tools", ToolNode(tools)) # equip the agents with the list of tools
|
| 79 |
+
|
| 80 |
+
# connect nodes - control flow
|
| 81 |
+
builder.add_edge(START, "retriever")
|
| 82 |
+
builder.add_edge("retriever", "assistant")
|
| 83 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
| 84 |
+
builder.add_edge("tools", "assistant")
|
| 85 |
+
|
| 86 |
+
# Compile graph
|
| 87 |
+
return builder.compile()
|