Spaces:
Runtime error
Runtime error
Delete agent.py
Browse files
agent.py
DELETED
|
@@ -1,140 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
from tools.python_interpreter import CodeInterpreter
|
| 4 |
-
|
| 5 |
-
interpreter_instance = CodeInterpreter()
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from tools.image import *
|
| 9 |
-
|
| 10 |
-
"""Langraph"""
|
| 11 |
-
from langgraph.graph import START, StateGraph, MessagesState
|
| 12 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 13 |
-
from langchain_groq import ChatGroq
|
| 14 |
-
from langchain_huggingface import (
|
| 15 |
-
ChatHuggingFace,
|
| 16 |
-
HuggingFaceEndpoint,
|
| 17 |
-
HuggingFaceEmbeddings,
|
| 18 |
-
)
|
| 19 |
-
from langchain_community.vectorstores import SupabaseVectorStore
|
| 20 |
-
from langchain_core.messages import SystemMessage, HumanMessage
|
| 21 |
-
from langchain_community.tools import create_retriever_tool
|
| 22 |
-
from supabase.client import Client, create_client
|
| 23 |
-
# ------- Tools
|
| 24 |
-
from tools.browse import web_search, wiki_search, arxiv_search
|
| 25 |
-
from tools.document_process import save_and_read_file, analyze_csv_file, analyze_excel_file, extract_text_from_image, download_file_from_url
|
| 26 |
-
from tools.image_tools import analyze_image, generate_simple_image , transform_image, draw_on_image, combine_images
|
| 27 |
-
from tools.simple_math import multiply, add, subtract, divide, modulus, power, square_root
|
| 28 |
-
from tools.python_interpreter import execute_code_lang
|
| 29 |
-
|
| 30 |
-
load_dotenv()
|
| 31 |
-
|
| 32 |
-
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 33 |
-
system_prompt = f.read()
|
| 34 |
-
print(system_prompt)
|
| 35 |
-
|
| 36 |
-
# System message
|
| 37 |
-
sys_msg = SystemMessage(content=system_prompt)
|
| 38 |
-
|
| 39 |
-
# build a retriever
|
| 40 |
-
embeddings = HuggingFaceEmbeddings(
|
| 41 |
-
model_name="sentence-transformers/all-mpnet-base-v2",
|
| 42 |
-
) # dim=768
|
| 43 |
-
supabase: Client = create_client(
|
| 44 |
-
os.environ.get("SUPABASE_URL_HUGGING_FACE"), os.environ.get("SUPABASE_SERVICE_ROLE_HUGGING_FACE")
|
| 45 |
-
)
|
| 46 |
-
vector_store = SupabaseVectorStore(
|
| 47 |
-
client=supabase,
|
| 48 |
-
embedding=embeddings,
|
| 49 |
-
table_name="documents",
|
| 50 |
-
query_name="match_documents_langchain",
|
| 51 |
-
)
|
| 52 |
-
create_retriever_tool = create_retriever_tool(
|
| 53 |
-
retriever=vector_store.as_retriever(),
|
| 54 |
-
name="Question Search",
|
| 55 |
-
description="A tool to retrieve similar questions from a vector store.",
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
tools = [
|
| 60 |
-
web_search,
|
| 61 |
-
wiki_search,
|
| 62 |
-
arxiv_search,
|
| 63 |
-
multiply,
|
| 64 |
-
add,
|
| 65 |
-
subtract,
|
| 66 |
-
divide,
|
| 67 |
-
modulus,
|
| 68 |
-
power,
|
| 69 |
-
square_root,
|
| 70 |
-
save_and_read_file,
|
| 71 |
-
download_file_from_url,
|
| 72 |
-
extract_text_from_image,
|
| 73 |
-
analyze_csv_file,
|
| 74 |
-
analyze_excel_file,
|
| 75 |
-
execute_code_lang,
|
| 76 |
-
analyze_image,
|
| 77 |
-
transform_image,
|
| 78 |
-
draw_on_image,
|
| 79 |
-
generate_simple_image,
|
| 80 |
-
combine_images,
|
| 81 |
-
]
|
| 82 |
-
|
| 83 |
-
def build_graph(provider: str = "groq"):
|
| 84 |
-
if provider == "groq":
|
| 85 |
-
# Groq https://console.groq.com/docs/models
|
| 86 |
-
llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
|
| 87 |
-
# llm = ChatGroq(model="deepseek-r1-distill-llama-70b", temperature=0)
|
| 88 |
-
elif provider == "huggingface":
|
| 89 |
-
llm = ChatHuggingFace(
|
| 90 |
-
llm=HuggingFaceEndpoint(
|
| 91 |
-
repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 92 |
-
task="text-generation", # for chat‐style use “text-generation”
|
| 93 |
-
max_new_tokens=1024,
|
| 94 |
-
do_sample=False,
|
| 95 |
-
repetition_penalty=1.03,
|
| 96 |
-
temperature=0,
|
| 97 |
-
),
|
| 98 |
-
verbose=True,
|
| 99 |
-
)
|
| 100 |
-
else:
|
| 101 |
-
raise ValueError("Invalid provider. Choose 'groq' or 'huggingface'.")
|
| 102 |
-
|
| 103 |
-
llm_with_tools = llm.bind_tools(tools)
|
| 104 |
-
|
| 105 |
-
def assistant(state: MessagesState):
|
| 106 |
-
"""Assistant Node"""
|
| 107 |
-
return {"messages": [llm_with_tools.invoke(state['messages'])]}
|
| 108 |
-
|
| 109 |
-
def retriever(state: MessagesState):
|
| 110 |
-
"""Retriever Node"""
|
| 111 |
-
# Extract the latest message content
|
| 112 |
-
query = state['messages'][-1].content
|
| 113 |
-
similar_question = vector_store.similarity_search(query, k = 2)
|
| 114 |
-
if similar_question:
|
| 115 |
-
example_msg = HumanMessage(
|
| 116 |
-
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
| 117 |
-
)
|
| 118 |
-
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
| 119 |
-
else:
|
| 120 |
-
return {"messages": [sys_msg] + state["messages"]}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
builder = StateGraph(MessagesState)
|
| 124 |
-
builder.add_node("retriever", retriever)
|
| 125 |
-
builder.add_node("assistant", assistant)
|
| 126 |
-
builder.add_node("tools", ToolNode(tools))
|
| 127 |
-
builder.add_edge(START, "retriever")
|
| 128 |
-
builder.add_edge("retriever", "assistant")
|
| 129 |
-
builder.add_conditional_edges("assistant", tools_condition)
|
| 130 |
-
builder.add_edge("tools", "assistant")
|
| 131 |
-
return builder.compile()
|
| 132 |
-
|
| 133 |
-
if __name__ == "__main__":
|
| 134 |
-
question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."
|
| 135 |
-
# question = """Q is Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec. What does Teal'c say in response to the question "Isn't that hot?"""
|
| 136 |
-
graph = build_graph(provider="groq")
|
| 137 |
-
messages = [HumanMessage(content=question)]
|
| 138 |
-
messages = graph.invoke({"messages": messages})
|
| 139 |
-
for m in messages["messages"]:
|
| 140 |
-
m.pretty_print()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|