Spaces:
Runtime error
Runtime error
baixianger commited on
Commit ยท
41cb4a2
1
Parent(s): 3526766
add retriever tool
Browse filesFiles changed (5)
- agent.py+70-15
- requirements.txt+7-1
- supabase_docs.csvAdded
- system_prompt.txt+3-1
- test.ipynb+214-45
12345678910111213141589 unmodified lines1051061071081091103 unmodified lines1141151161171181191201211221231241251261271 unmodified line1291301311321331341351361371381391401411427 unmodified lines150151152"""LangGraph Agent"""import dotenvfrom langgraph.graph import MessagesStatefrom langgraph.graph import START, StateGraphfrom langgraph.prebuilt import tools_conditionfrom langgraph.prebuilt import ToolNodefrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_community.tools.tavily_search import TavilySearchResultsfrom langchain_community.document_loaders import WikipediaLoaderfrom langchain_community.document_loaders import ArxivLoaderfrom langchain_core.tools import toolfrom langchain_core.messages import SystemMessage@tooldef multiply(a: int, b: int) -> int:89 unmodified lines])return {"arvix_results": formatted_search_docs}tools = [multiply,add,3 unmodified lineswiki_search,web_search,arvix_search,]# Load environment variables from .env filedotenv.load_dotenv()llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")llm_with_tools = llm.bind_tools(tools)# load the system prompt from the filewith open("system_prompt.txt", "r", encoding="utf-8") as f:system_prompt = f.read()1 unmodified line# System messagesys_msg = SystemMessage(content=system_prompt)# Nodedef assistant(state: MessagesState):"""Assistant node"""return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}# Build graph functiondef build_graph():"""Build the graph"""builder = StateGraph(MessagesState)builder.add_node("assistant", assistant)7 unmodified lines# Compile graphreturn builder.compile()1234567891011121314151617181920212289 unmodified lines1121131141151161171181191201211221231241251261271281291301311321331341351363 unmodified lines1401411421431441451461471481 unmodified line1501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831847 unmodified lines192193194195196197198199200201202203204205206207"""LangGraph Agent"""import osfrom dotenv import load_dotenvfrom langgraph.graph import START, StateGraph, MessagesStatefrom langgraph.prebuilt import tools_conditionfrom langgraph.prebuilt import ToolNodefrom langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_groq import ChatGroqfrom langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddingsfrom langchain_community.tools.tavily_search import TavilySearchResultsfrom langchain_community.document_loaders import WikipediaLoaderfrom langchain_community.document_loaders import ArxivLoaderfrom langchain_community.vectorstores import SupabaseVectorStorefrom langchain_core.messages import SystemMessage, HumanMessagefrom langchain_core.tools import toolfrom langchain.tools.retriever import create_retriever_toolfrom supabase.client import Client, create_clientload_dotenv()@tooldef multiply(a: int, b: int) -> int:89 unmodified lines])return {"arvix_results": formatted_search_docs}# build a retriever toolembeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") # dim=768supabase: Client = create_client(os.environ.get("SUPABASE_URL"),os.environ.get("SUPABASE_SERVICE_KEY"))vector_store = SupabaseVectorStore(client=supabase,embedding= embeddings,table_name="documents",query_name="match_documents_langchain",)question_retrieve_tool = create_retriever_tool(vector_store.as_retriever(),"Question Retriever","Find similar questions in the vector database for the given question.",)tools = [multiply,add,3 unmodified lineswiki_search,web_search,arvix_search,question_retrieve_tool]# load the system prompt from the filewith open("system_prompt.txt", "r", encoding="utf-8") as f:system_prompt = f.read()1 unmodified line# System messagesys_msg = SystemMessage(content=system_prompt)# Build graph functiondef build_graph(provider: str = "groq"):"""Build the graph"""# Load environment variables from .env fileif provider == "google":# Google Geminillm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)elif provider == "groq":# Groq https://console.groq.com/docs/modelsllm = ChatGroq(model="gemma2-9b-it", temperature=0) # optional : qwen-qwq-32belif provider == "huggingface":# TODO: Add huggingface endpointllm = ChatHuggingFace(llm=HuggingFaceEndpoint(url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",temperature=0,),)else:raise ValueError("Invalid provider. Choose 'google', 'groq' or 'huggingface'.")# Bind tools to LLMllm_with_tools = llm.bind_tools(tools)# Nodedef assistant(state: MessagesState):"""Assistant node"""return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}builder = StateGraph(MessagesState)builder.add_node("assistant", assistant)7 unmodified lines# Compile graphreturn builder.compile()# testif __name__ == "__main__":question = "Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?"# Build the graphgraph = build_graph(provider="groq")# Run the graphmessages = [HumanMessage(content=question)]messages = graph.invoke({"messages": messages})answer = messages[-1].contentprint(f"Question: {question}")print(f"{answer}")
3 unmodified lines4567891011123 unmodified lineslangchain-communitylangchain-corelangchain-google-genailangchain-tavilylanggrapharxivpymupdfwikipediadotenv3 unmodified lines4567891011121314151617183 unmodified lineslangchain-communitylangchain-corelangchain-google-genailangchain-huggingfacelangchain-groqlangchain-tavilylangchain-chromalanggraphhuggingface_hubsupabasearxivpymupdfwikipediapgvectorpython-dotenv
32 unmodified lines3334353632 unmodified linesFinal Answer: Rd5==========================Now, I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.32 unmodified lines33343536373832 unmodified linesFinal Answer: Rd5==========================Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:FINAL ANSWER: [YOUR FINAL ANSWER].YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
1234567812 unmodified lines212223242526272 unmodified lines3031323334353637383940414243444546474849505152535455565758596061626364656667686970717273745 unmodified lines8081828384858613 unmodified lines100101102103104105110 unmodified lines21621721821922022179 unmodified lines30130230330430530630730830931031131231331431531631731831932032132289 unmodified lines4124134144154164173 unmodified lines42142242342442542642742842943043157 unmodified lines489490491492493494{"cells": [{"cell_type": "code","execution_count": 50,"id": "14e3f417","metadata": {},"outputs": [],12 unmodified lines},{"cell_type": "code","execution_count": 51,"id": "5e2da6fc","metadata": {},"outputs": [2 unmodified lines"output_type": "stream","text": ["==================================================\n","Task ID: f0f46385-fc03-4599-b5d3-f56496c3e69f\n","Question: In terms of geographical distance between capital cities, which 2 countries are the furthest from each other within the ASEAN bloc according to wikipedia? Answer using a comma separated list, ordering the countries by alphabetical order.\n","Level: 2\n","Final Answer: Indonesia, Myanmar\n","Annotator Metadata: \n"," โโโ Steps: \n"," โ โโโ 1. Search the web for \"ASEAN bloc\".\n"," โ โโโ 2. Click the Wikipedia result for the ASEAN Free Trade Area.\n"," โ โโโ 3. Scroll down to find the list of member states.\n"," โ โโโ 4. Click into the Wikipedia pages for each member state, and note its capital.\n"," โ โโโ 5. Search the web for the distance between the first two capitals. The results give travel distance, not geographic distance, which might affect the answer.\n"," โ โโโ 6. Thinking it might be faster to judge the distance by looking at a map, search the web for \"ASEAN bloc\" and click into the images tab.\n"," โ โโโ 7. View a map of the member countries. Since they're clustered together in an arrangement that's not very linear, it's difficult to judge distances by eye.\n"," โ โโโ 8. Return to the Wikipedia page for each country. Click the GPS coordinates for each capital to get the coordinates in decimal notation.\n"," โ โโโ 9. Place all these coordinates into a spreadsheet.\n"," โ โโโ 10. Write formulas to calculate the distance between each capital.\n"," โ โโโ 11. Write formula to get the largest distance value in the spreadsheet.\n"," โ โโโ 12. Note which two capitals that value corresponds to: Jakarta and Naypyidaw.\n"," โ โโโ 13. Return to the Wikipedia pages to see which countries those respective capitals belong to: Indonesia, Myanmar.\n"," โโโ Number of steps: 13\n"," โโโ How long did this take?: 45 minutes\n"," โโโ Tools:\n"," โ โโโ 1. Search engine\n"," โ โโโ 2. Web browser\n"," โ โโโ 3. Microsoft Excel / Google Sheets\n"," โโโ Number of tools: 3\n","==================================================\n","Task ID: cca530fc-4052-43b2-b130-b30968d8aa44\n","Question: Review the chess position provided in the image. It is black's turn. Provide the correct next move for black which guarantees a win. Please provide your response in algebraic notation.\n","Level: 1\n","Final Answer: Rd5\n","Annotator Metadata: \n"," โโโ Steps: \n"," โ โโโ Step 1: Evaluate the position of the pieces in the chess position\n"," โ โโโ Step 2: Report the best move available for black: \"Rd5\"\n"," โโโ Number of steps: 2\n"," โโโ How long did this take?: 10 minutes\n"," โโโ Tools:\n"," โ โโโ 1. Image recognition tools\n"," โโโ Number of tools: 1\n","==================================================\n"]5 unmodified lines"\n","import random\n","# random.seed(42)\n","random_samples = random.sample(json_QA, 2)\n","for sample in random_samples:\n"," print(\"=\" * 50)\n"," print(f\"Task ID: {sample['task_id']}\")\n",13 unmodified lines"print(\"=\" * 50)"]},{"cell_type": "code","execution_count": 31,110 unmodified lines" print(f\" โโโ {tool}: {count}\")"]},{"cell_type": "code","execution_count": 55,79 unmodified lines},{"cell_type": "code","execution_count": 46,"id": "42fde0f8","metadata": {},"outputs": [],"source": ["import dotenv\n","from langgraph.graph import MessagesState\n","from langchain_core.messages import HumanMessage, SystemMessage\n","from langgraph.graph import START, StateGraph\n","from langgraph.prebuilt import tools_condition\n","from langgraph.prebuilt import ToolNode\n","from langchain_google_genai import ChatGoogleGenerativeAI\n","from langchain_community.tools.tavily_search import TavilySearchResults\n","from langchain_community.document_loaders import WikipediaLoader\n","from langchain_community.document_loaders import ArxivLoader\n","from langchain_core.tools import tool\n","\n","@tool\n","def multiply(a: int, b: int) -> int:\n",89 unmodified lines" ])\n"," return {\"arvix_results\": formatted_search_docs}\n","\n","tools = [\n"," multiply,\n"," add,\n",3 unmodified lines" wiki_search,\n"," web_search,\n"," arvix_search,\n","]\n","\n","\n","# Load environment variables from .env file\n","dotenv.load_dotenv()\n","llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\")\n","llm_with_tools = llm.bind_tools(tools)"]57 unmodified lines"\n","display(Image(graph.get_graph(xray=True).draw_mermaid_png()))"]}],"metadata": {1234567891011121314151612 unmodified lines293031323334352 unmodified lines383940414243444546474849505152535455565758596061626364655 unmodified lines7172737475767713 unmodified lines919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208110 unmodified lines31932032132232332432532632732832933033133279 unmodified lines41241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545689 unmodified lines5465475485495505515525535545555565575585595605615625635645653 unmodified lines56957057157257357457557657757 unmodified lines635636637638639640641642643644645646647648649650651652653654655656657658659660661662663{"cells": [{"cell_type": "markdown","id": "d0cc4adf","metadata": {},"source": ["### Question data"]},{"cell_type": "code","execution_count": 2,"id": "14e3f417","metadata": {},"outputs": [],12 unmodified lines},{"cell_type": "code","execution_count": 3,"id": "5e2da6fc","metadata": {},"outputs": [2 unmodified lines"output_type": "stream","text": ["==================================================\n","Task ID: ed58682d-bc52-4baa-9eb0-4eb81e1edacc\n","Question: What is the last word before the second chorus of the King of Pop's fifth single from his sixth studio album?\n","Level: 2\n","Final Answer: stare\n","Annotator Metadata: \n"," โโโ Steps: \n"," โ โโโ 1. Google searched \"King of Pop\".\n"," โ โโโ 2. Clicked on Michael Jackson's Wikipedia.\n"," โ โโโ 3. Scrolled down to \"Discography\".\n"," โ โโโ 4. Clicked on the sixth album, \"Thriller\".\n"," โ โโโ 5. Looked under \"Singles from Thriller\".\n"," โ โโโ 6. Clicked on the fifth single, \"Human Nature\".\n"," โ โโโ 7. Google searched \"Human Nature Michael Jackson Lyrics\".\n"," โ โโโ 8. Looked at the opening result with full lyrics sourced by Musixmatch.\n"," โ โโโ 9. Looked for repeating lyrics to determine the chorus.\n"," โ โโโ 10. Determined the chorus begins with \"If they say\" and ends with \"Does he do me that way?\"\n"," โ โโโ 11. Found the second instance of the chorus within the lyrics.\n"," โ โโโ 12. Noted the last word before the second chorus - \"stare\".\n"," โโโ Number of steps: 12\n"," โโโ How long did this take?: 20 minutes\n"," โโโ Tools:\n"," โ โโโ Web Browser\n"," โโโ Number of tools: 1\n","==================================================\n"]5 unmodified lines"\n","import random\n","# random.seed(42)\n","random_samples = random.sample(json_QA, 1)\n","for sample in random_samples:\n"," print(\"=\" * 50)\n"," print(f\"Task ID: {sample['task_id']}\")\n",13 unmodified lines"print(\"=\" * 50)"]},{"cell_type": "code","execution_count": 48,"id": "4bb02420","metadata": {},"outputs": [],"source": ["### build a vector database based on the metadata.jsonl\n","# https://python.langchain.com/docs/integrations/vectorstores/supabase/\n","import os\n","from dotenv import load_dotenv\n","from langchain_huggingface import HuggingFaceEmbeddings\n","from langchain_community.vectorstores import SupabaseVectorStore\n","from supabase.client import Client, create_client\n","\n","\n","load_dotenv()\n","embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\") # dim=768\n","\n","supabase_url = os.environ.get(\"SUPABASE_URL\")\n","supabase_key = os.environ.get(\"SUPABASE_SERVICE_KEY\")\n","supabase: Client = create_client(supabase_url, supabase_key)"]},{"cell_type": "code","execution_count": null,"id": "a070b955","metadata": {},"outputs": [],"source": ["# wrap the metadata.jsonl's questions and answers into a list of document\n","from langchain.schema import Document\n","docs = []\n","for sample in json_QA:\n"," content = f\"Question : {sample['Question']}\\n\\nFinal answer : {sample['Final answer']}\"\n"," doc = {\n"," \"content\" : content,\n"," \"metadata\" : { # meatadata็ๆ ผๅผๅฟ ้กปๆถsource้ฎ๏ผๅฆๅไผๆฅ้\n"," \"source\" : sample['task_id']\n"," },\n"," \"embedding\" : embeddings.embed_query(content),\n"," }\n"," docs.append(doc)\n","\n","# upload the documents to the vector database\n","try:\n"," response = (\n"," supabase.table(\"documents\")\n"," .insert(docs)\n"," .execute()\n"," )\n","except Exception as exception:\n"," print(\"Error inserting data into Supabase:\", exception)\n","\n","# ALTERNATIVE : Save the documents (a list of dict) into a csv file, and manually upload it to Supabase\n","# import pandas as pd\n","# df = pd.DataFrame(docs)\n","# df.to_csv('supabase_docs.csv', index=False)"]},{"cell_type": "code","execution_count": 54,"id": "77fb9dbb","metadata": {},"outputs": [],"source": ["# add items to vector database\n","vector_store = SupabaseVectorStore(\n"," client=supabase,\n"," embedding= embeddings,\n"," table_name=\"documents\",\n"," query_name=\"match_documents_langchain\",\n",")\n","retriever = vector_store.as_retriever()"]},{"cell_type": "code","execution_count": 55,"id": "12a05971","metadata": {},"outputs": [{"name": "stderr","output_type": "stream","text": ["huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n","To disable this warning, you can either:\n","\t- Avoid using `tokenizers` before the fork if possible\n","\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"]},{"data": {"text/plain": ["Document(metadata={'source': '840bfca7-4f7b-481a-8794-c560c340185d'}, page_content='Question : On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\\n\\nFinal answer : 80GSFC21M0002')"]},"execution_count": 55,"metadata": {},"output_type": "execute_result"}],"source": ["query = \"On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?\"\n","# matched_docs = vector_store.similarity_search(query, 2)\n","docs = retriever.invoke(query)\n","docs[0]"]},{"cell_type": "code","execution_count": 31,110 unmodified lines" print(f\" โโโ {tool}: {count}\")"]},{"cell_type": "markdown","id": "5efee12a","metadata": {},"source": ["#### Graph"]},{"cell_type": "code","execution_count": 55,79 unmodified lines},{"cell_type": "code","execution_count": null,"id": "42fde0f8","metadata": {},"outputs": [],"source": ["import dotenv\n","from langgraph.graph import MessagesState, START, StateGraph\n","from langgraph.prebuilt import tools_condition\n","from langgraph.prebuilt import ToolNode\n","from langchain_google_genai import ChatGoogleGenerativeAI\n","from langchain_huggingface import HuggingFaceEmbeddings\n","from langchain_community.tools.tavily_search import TavilySearchResults\n","from langchain_community.document_loaders import WikipediaLoader\n","from langchain_community.document_loaders import ArxivLoader\n","from langchain_community.vectorstores import SupabaseVectorStore\n","from langchain.tools.retriever import create_retriever_tool\n","from langchain_core.messages import HumanMessage, SystemMessage\n","from langchain_core.tools import tool\n","from supabase.client import Client, create_client\n","\n","# Define the retriever from supabase\n","load_dotenv()\n","embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\") # dim=768\n","\n","supabase_url = os.environ.get(\"SUPABASE_URL\")\n","supabase_key = os.environ.get(\"SUPABASE_SERVICE_KEY\")\n","supabase: Client = create_client(supabase_url, supabase_key)\n","vector_store = SupabaseVectorStore(\n"," client=supabase,\n"," embedding= embeddings,\n"," table_name=\"documents\",\n"," query_name=\"match_documents_langchain\",\n",")\n","\n","question_retrieve_tool = create_retriever_tool(\n"," vector_store.as_retriever(),\n"," \"Question Retriever\",\n"," \"Find similar questions in the vector database for the given question.\",\n",")\n","\n","@tool\n","def multiply(a: int, b: int) -> int:\n",89 unmodified lines" ])\n"," return {\"arvix_results\": formatted_search_docs}\n","\n","@tool\n","def similar_question_search(question: str) -> str:\n"," \"\"\"Search the vector database for similar questions and return the first results.\n"," \n"," Args:\n"," question: the question human provided.\"\"\"\n"," matched_docs = vector_store.similarity_search(query, 3)\n"," formatted_search_docs = \"\\n\\n---\\n\\n\".join(\n"," [\n"," f'<Document source=\"{doc.metadata[\"source\"]}\" page=\"{doc.metadata.get(\"page\", \"\")}\"/>\\n{doc.page_content[:1000]}\\n</Document>'\n"," for doc in matched_docs\n"," ])\n"," return {\"similar_questions\": formatted_search_docs}\n","\n","tools = [\n"," multiply,\n"," add,\n",3 unmodified lines" wiki_search,\n"," web_search,\n"," arvix_search,\n"," question_retrieve_tool\n","]\n","\n","llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\")\n","llm_with_tools = llm.bind_tools(tools)"]57 unmodified lines"\n","display(Image(graph.get_graph(xray=True).draw_mermaid_png()))"]},{"cell_type": "code","execution_count": null,"id": "5987d58c","metadata": {},"outputs": [],"source": ["question = \"\"\n","messages = [HumanMessage(content=question)]\n","messages = graph.invoke({\"messages\": messages})"]},{"cell_type": "code","execution_count": null,"id": "330cbf17","metadata": {},"outputs": [],"source": ["for m in messages['messages']:\n"," m.pretty_print()"]}],"metadata": {
agent.py+70-15
requirements.txt+7-1
supabase_docs.csvAdded
system_prompt.txt+3-1
test.ipynb+214-45