Commit
·
e753b9f
1
Parent(s):
33426c9
+llamaindex model changed to langgraph
Browse files- README_SUPABASE.md +48 -0
- agent.ipynb +341 -0
- app.py +3 -2
- chat_models_check.py +5 -0
- langraph_agent.py +214 -0
- pyproject.toml +12 -1
- test.py +209 -0
- uv.lock +0 -0
README_SUPABASE.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
command to run to activate pgvector extgension which is required to create a vector store on supabase
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
note: change the embedding size to your model here it is 1536
|
| 5 |
+
|
| 6 |
+
[ref](https://js.langchain.com/docs/integrations/vectorstores/supabase/)
|
| 7 |
+
```
|
| 8 |
+
-- Enable the pgvector extension to work with embedding vectors
|
| 9 |
+
create extension vector;
|
| 10 |
+
|
| 11 |
+
-- Create a table to store your documents
|
| 12 |
+
create table documents (
|
| 13 |
+
id bigserial primary key,
|
| 14 |
+
content text, -- corresponds to Document.pageContent
|
| 15 |
+
metadata jsonb, -- corresponds to Document.metadata
|
| 16 |
+
embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
|
| 17 |
+
);
|
| 18 |
+
|
| 19 |
+
-- Create a function to search for documents
|
| 20 |
+
create function match_documents (
|
| 21 |
+
query_embedding vector(1536),
|
| 22 |
+
match_count int DEFAULT null,
|
| 23 |
+
filter jsonb DEFAULT '{}'
|
| 24 |
+
) returns table (
|
| 25 |
+
id bigint,
|
| 26 |
+
content text,
|
| 27 |
+
metadata jsonb,
|
| 28 |
+
embedding jsonb,
|
| 29 |
+
similarity float
|
| 30 |
+
)
|
| 31 |
+
language plpgsql
|
| 32 |
+
as $$
|
| 33 |
+
#variable_conflict use_column
|
| 34 |
+
begin
|
| 35 |
+
return query
|
| 36 |
+
select
|
| 37 |
+
id,
|
| 38 |
+
content,
|
| 39 |
+
metadata,
|
| 40 |
+
(embedding::text)::jsonb as embedding,
|
| 41 |
+
1 - (documents.embedding <=> query_embedding) as similarity
|
| 42 |
+
from documents
|
| 43 |
+
where metadata @> filter
|
| 44 |
+
order by documents.embedding <=> query_embedding
|
| 45 |
+
limit match_count;
|
| 46 |
+
end;
|
| 47 |
+
$$;
|
| 48 |
+
```
|
agent.ipynb
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import os\n",
|
| 10 |
+
"from dotenv import load_dotenv\n",
|
| 11 |
+
"from langgraph.graph import START, StateGraph, MessagesState\n",
|
| 12 |
+
"from langgraph.prebuilt import tools_condition\n",
|
| 13 |
+
"from langgraph.prebuilt import ToolNode\n",
|
| 14 |
+
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 15 |
+
"from langchain_groq import ChatGroq\n",
|
| 16 |
+
"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings\n",
|
| 17 |
+
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
|
| 18 |
+
"from langchain_community.document_loaders import WikipediaLoader\n",
|
| 19 |
+
"from langchain_community.document_loaders import ArxivLoader\n",
|
| 20 |
+
"from langchain_community.vectorstores import SupabaseVectorStore\n",
|
| 21 |
+
"from langchain_core.messages import SystemMessage, HumanMessage\n",
|
| 22 |
+
"from langchain_core.tools import tool\n",
|
| 23 |
+
"from langchain.tools.retriever import create_retriever_tool\n",
|
| 24 |
+
"from supabase.client import Client, create_client"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "code",
|
| 29 |
+
"execution_count": 2,
|
| 30 |
+
"metadata": {},
|
| 31 |
+
"outputs": [
|
| 32 |
+
{
|
| 33 |
+
"data": {
|
| 34 |
+
"text/plain": [
|
| 35 |
+
"True"
|
| 36 |
+
]
|
| 37 |
+
},
|
| 38 |
+
"execution_count": 2,
|
| 39 |
+
"metadata": {},
|
| 40 |
+
"output_type": "execute_result"
|
| 41 |
+
}
|
| 42 |
+
],
|
| 43 |
+
"source": [
|
| 44 |
+
"load_dotenv(\"./env.local\")"
|
| 45 |
+
]
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"cell_type": "code",
|
| 49 |
+
"execution_count": 3,
|
| 50 |
+
"metadata": {},
|
| 51 |
+
"outputs": [],
|
| 52 |
+
"source": [
|
| 53 |
+
"@tool\n",
|
| 54 |
+
"def multiply(a: int, b: int) -> int:\n",
|
| 55 |
+
" \"\"\"Multiply two numbers.\n",
|
| 56 |
+
"\n",
|
| 57 |
+
" Args:\n",
|
| 58 |
+
" a: first int\n",
|
| 59 |
+
" b: second int\n",
|
| 60 |
+
" \"\"\"\n",
|
| 61 |
+
" return a * b\n",
|
| 62 |
+
"\n",
|
| 63 |
+
"@tool\n",
|
| 64 |
+
"def add(a: int, b: int) -> int:\n",
|
| 65 |
+
" \"\"\"Add two numbers.\n",
|
| 66 |
+
" \n",
|
| 67 |
+
" Args:\n",
|
| 68 |
+
" a: first int\n",
|
| 69 |
+
" b: second int\n",
|
| 70 |
+
" \"\"\"\n",
|
| 71 |
+
" return a + b\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"@tool\n",
|
| 74 |
+
"def subtract(a: int, b: int) -> int:\n",
|
| 75 |
+
" \"\"\"Subtract two numbers.\n",
|
| 76 |
+
" \n",
|
| 77 |
+
" Args:\n",
|
| 78 |
+
" a: first int\n",
|
| 79 |
+
" b: second int\n",
|
| 80 |
+
" \"\"\"\n",
|
| 81 |
+
" return a - b\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"@tool\n",
|
| 84 |
+
"def divide(a: int, b: int) -> int:\n",
|
| 85 |
+
" \"\"\"Divide two numbers.\n",
|
| 86 |
+
" \n",
|
| 87 |
+
" Args:\n",
|
| 88 |
+
" a: first int\n",
|
| 89 |
+
" b: second int\n",
|
| 90 |
+
" \"\"\"\n",
|
| 91 |
+
" if b == 0:\n",
|
| 92 |
+
" raise ValueError(\"Cannot divide by zero.\")\n",
|
| 93 |
+
" return a / b\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"@tool\n",
|
| 96 |
+
"def modulus(a: int, b: int) -> int:\n",
|
| 97 |
+
" \"\"\"Get the modulus of two numbers.\n",
|
| 98 |
+
" \n",
|
| 99 |
+
" Args:\n",
|
| 100 |
+
" a: first int\n",
|
| 101 |
+
" b: second int\n",
|
| 102 |
+
" \"\"\"\n",
|
| 103 |
+
" return a % b\n",
|
| 104 |
+
"\n",
|
| 105 |
+
"@tool\n",
|
| 106 |
+
"def wiki_search(query: str) -> str:\n",
|
| 107 |
+
" \"\"\"Search Wikipedia for a query and return maximum 2 results.\n",
|
| 108 |
+
" \n",
|
| 109 |
+
" Args:\n",
|
| 110 |
+
" query: The search query.\"\"\"\n",
|
| 111 |
+
" search_docs = WikipediaLoader(query=query, load_max_docs=2).load()\n",
|
| 112 |
+
" formatted_search_docs = \"\\n\\n---\\n\\n\".join(\n",
|
| 113 |
+
" [\n",
|
| 114 |
+
" f'<Document source=\"{doc.metadata[\"source\"]}\" page=\"{doc.metadata.get(\"page\", \"\")}\"/>\\n{doc.page_content}\\n</Document>'\n",
|
| 115 |
+
" for doc in search_docs\n",
|
| 116 |
+
" ])\n",
|
| 117 |
+
" return {\"wiki_results\": formatted_search_docs}\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"@tool\n",
|
| 120 |
+
"def web_search(query: str) -> str:\n",
|
| 121 |
+
" \"\"\"Search Tavily for a query and return maximum 3 results.\n",
|
| 122 |
+
" \n",
|
| 123 |
+
" Args:\n",
|
| 124 |
+
" query: The search query.\"\"\"\n",
|
| 125 |
+
" search_docs = TavilySearchResults(max_results=3).invoke(query=query)\n",
|
| 126 |
+
" formatted_search_docs = \"\\n\\n---\\n\\n\".join(\n",
|
| 127 |
+
" [\n",
|
| 128 |
+
" f'<Document source=\"{doc.metadata[\"source\"]}\" page=\"{doc.metadata.get(\"page\", \"\")}\"/>\\n{doc.page_content}\\n</Document>'\n",
|
| 129 |
+
" for doc in search_docs\n",
|
| 130 |
+
" ])\n",
|
| 131 |
+
" return {\"web_results\": formatted_search_docs}\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"@tool\n",
|
| 134 |
+
"def arvix_search(query: str) -> str:\n",
|
| 135 |
+
" \"\"\"Search Arxiv for a query and return maximum 3 result.\n",
|
| 136 |
+
" \n",
|
| 137 |
+
" Args:\n",
|
| 138 |
+
" query: The search query.\"\"\"\n",
|
| 139 |
+
" search_docs = ArxivLoader(query=query, load_max_docs=3).load()\n",
|
| 140 |
+
" formatted_search_docs = \"\\n\\n---\\n\\n\".join(\n",
|
| 141 |
+
" [\n",
|
| 142 |
+
" f'<Document source=\"{doc.metadata[\"source\"]}\" page=\"{doc.metadata.get(\"page\", \"\")}\"/>\\n{doc.page_content[:1000]}\\n</Document>'\n",
|
| 143 |
+
" for doc in search_docs\n",
|
| 144 |
+
" ])\n",
|
| 145 |
+
" return {\"arvix_results\": formatted_search_docs}\n"
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"cell_type": "code",
|
| 150 |
+
"execution_count": 4,
|
| 151 |
+
"metadata": {},
|
| 152 |
+
"outputs": [],
|
| 153 |
+
"source": [
|
| 154 |
+
"# load the system prompt from the file\n",
|
| 155 |
+
"with open(\"system_prompt.txt\", \"r\", encoding=\"utf-8\") as f:\n",
|
| 156 |
+
" system_prompt = f.read()\n",
|
| 157 |
+
"\n",
|
| 158 |
+
"# System message\n",
|
| 159 |
+
"sys_msg = SystemMessage(content=system_prompt)"
|
| 160 |
+
]
|
| 161 |
+
},
|
| 162 |
+
{
|
| 163 |
+
"cell_type": "code",
|
| 164 |
+
"execution_count": 5,
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"outputs": [],
|
| 167 |
+
"source": [
|
| 168 |
+
"# build a retriever\n",
|
| 169 |
+
"embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\") # dim=768\n",
|
| 170 |
+
"supabase: Client = create_client(\n",
|
| 171 |
+
" os.environ.get(\"SUPABASE_URL\"), \n",
|
| 172 |
+
" os.environ.get(\"SUPABASE_SERVICE_KEY\"))\n",
|
| 173 |
+
"vector_store = SupabaseVectorStore(\n",
|
| 174 |
+
" client=supabase,\n",
|
| 175 |
+
" embedding= embeddings,\n",
|
| 176 |
+
" table_name=\"documents\",\n",
|
| 177 |
+
" query_name=\"match_documents_langchain\",\n",
|
| 178 |
+
")\n",
|
| 179 |
+
"create_retriever_tool = create_retriever_tool(\n",
|
| 180 |
+
" retriever=vector_store.as_retriever(),\n",
|
| 181 |
+
" name=\"Question Search\",\n",
|
| 182 |
+
" description=\"A tool to retrieve similar questions from a vector store.\",\n",
|
| 183 |
+
")\n"
|
| 184 |
+
]
|
| 185 |
+
},
|
| 186 |
+
{
|
| 187 |
+
"cell_type": "code",
|
| 188 |
+
"execution_count": 6,
|
| 189 |
+
"metadata": {},
|
| 190 |
+
"outputs": [],
|
| 191 |
+
"source": [
|
| 192 |
+
"tools = [\n",
|
| 193 |
+
" multiply,\n",
|
| 194 |
+
" add,\n",
|
| 195 |
+
" subtract,\n",
|
| 196 |
+
" divide,\n",
|
| 197 |
+
" modulus,\n",
|
| 198 |
+
" wiki_search,\n",
|
| 199 |
+
" web_search,\n",
|
| 200 |
+
" arvix_search,\n",
|
| 201 |
+
"]\n"
|
| 202 |
+
]
|
| 203 |
+
},
|
| 204 |
+
{
|
| 205 |
+
"cell_type": "code",
|
| 206 |
+
"execution_count": 14,
|
| 207 |
+
"metadata": {},
|
| 208 |
+
"outputs": [],
|
| 209 |
+
"source": [
|
| 210 |
+
"# Build graph function\n",
|
| 211 |
+
"def build_graph(provider: str = \"groq\"):\n",
|
| 212 |
+
" \"\"\"Build the graph\"\"\"\n",
|
| 213 |
+
" # Load environment variables from .env file\n",
|
| 214 |
+
" if provider == \"google\":\n",
|
| 215 |
+
" # Google Gemini\n",
|
| 216 |
+
" llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\", temperature=0)\n",
|
| 217 |
+
" elif provider == \"groq\":\n",
|
| 218 |
+
" # Groq https://console.groq.com/docs/models\n",
|
| 219 |
+
" llm = ChatGroq(model=\"qwen-qwq-32b\", temperature=0) # optional : qwen-qwq-32b gemma2-9b-it\n",
|
| 220 |
+
" elif provider == \"huggingface\":\n",
|
| 221 |
+
" # TODO: Add huggingface endpoint\n",
|
| 222 |
+
" llm = ChatHuggingFace(\n",
|
| 223 |
+
" llm=HuggingFaceEndpoint(\n",
|
| 224 |
+
" url=\"https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf\",\n",
|
| 225 |
+
" temperature=0,\n",
|
| 226 |
+
" ),\n",
|
| 227 |
+
" )\n",
|
| 228 |
+
" else:\n",
|
| 229 |
+
" raise ValueError(\"Invalid provider. Choose 'google', 'groq' or 'huggingface'.\")\n",
|
| 230 |
+
" # Bind tools to LLM\n",
|
| 231 |
+
" llm_with_tools = llm.bind_tools(tools)\n",
|
| 232 |
+
"\n",
|
| 233 |
+
" # Node\n",
|
| 234 |
+
" def assistant(state: MessagesState):\n",
|
| 235 |
+
" \"\"\"Assistant node\"\"\"\n",
|
| 236 |
+
" return {\"messages\": [llm_with_tools.invoke(state[\"messages\"])]}\n",
|
| 237 |
+
" \n",
|
| 238 |
+
" def retriever(state: MessagesState):\n",
|
| 239 |
+
" \"\"\"Retriever node\"\"\"\n",
|
| 240 |
+
" similar_question = vector_store.similarity_search(state[\"messages\"][0].content)\n",
|
| 241 |
+
" if similar_question:\n",
|
| 242 |
+
" example_msg = HumanMessage(\n",
|
| 243 |
+
" content=f\"Here I provide a similar question and answer for reference: \\n\\n{similar_question[0].page_content}\",\n",
|
| 244 |
+
" )\n",
|
| 245 |
+
" else:\n",
|
| 246 |
+
" example_msg = HumanMessage(\n",
|
| 247 |
+
" content=\"No similar question found in the database.\"\n",
|
| 248 |
+
" )\n",
|
| 249 |
+
" return {\"messages\": [sys_msg] + state[\"messages\"] + [example_msg]}\n",
|
| 250 |
+
"\n",
|
| 251 |
+
" builder = StateGraph(MessagesState)\n",
|
| 252 |
+
" builder.add_node(\"retriever\", retriever)\n",
|
| 253 |
+
" builder.add_node(\"assistant\", assistant)\n",
|
| 254 |
+
" builder.add_node(\"tools\", ToolNode(tools))\n",
|
| 255 |
+
" builder.add_edge(START, \"retriever\")\n",
|
| 256 |
+
" builder.add_edge(\"retriever\", \"assistant\")\n",
|
| 257 |
+
" builder.add_conditional_edges(\n",
|
| 258 |
+
" \"assistant\",\n",
|
| 259 |
+
" tools_condition,\n",
|
| 260 |
+
" )\n",
|
| 261 |
+
" builder.add_edge(\"tools\", \"assistant\")\n",
|
| 262 |
+
"\n",
|
| 263 |
+
" # Compile graph\n",
|
| 264 |
+
" return builder.compile()"
|
| 265 |
+
]
|
| 266 |
+
},
|
| 267 |
+
{
|
| 268 |
+
"cell_type": "code",
|
| 269 |
+
"execution_count": 16,
|
| 270 |
+
"metadata": {},
|
| 271 |
+
"outputs": [
|
| 272 |
+
{
|
| 273 |
+
"ename": "PermissionDeniedError",
|
| 274 |
+
"evalue": "Error code: 403 - {'error': {'message': 'Access denied. Please check your network settings.'}}",
|
| 275 |
+
"output_type": "error",
|
| 276 |
+
"traceback": [
|
| 277 |
+
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
| 278 |
+
"\u001b[31mPermissionDeniedError\u001b[39m Traceback (most recent call last)",
|
| 279 |
+
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[16]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[32m 4\u001b[39m \u001b[38;5;66;03m# Run the graph\u001b[39;00m\n\u001b[32m 5\u001b[39m messages = [HumanMessage(content=question)]\n\u001b[32m----> \u001b[39m\u001b[32m6\u001b[39m messages = \u001b[43mgraph\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43m{\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmessages\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 7\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m m \u001b[38;5;129;01min\u001b[39;00m messages[\u001b[33m\"\u001b[39m\u001b[33mmessages\u001b[39m\u001b[33m\"\u001b[39m]:\n\u001b[32m 8\u001b[39m m.pretty_print()\n",
|
| 280 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langgraph\\pregel\\__init__.py:2719\u001b[39m, in \u001b[36mPregel.invoke\u001b[39m\u001b[34m(self, input, config, stream_mode, output_keys, interrupt_before, interrupt_after, checkpoint_during, debug, **kwargs)\u001b[39m\n\u001b[32m 2716\u001b[39m chunks: \u001b[38;5;28mlist\u001b[39m[Union[\u001b[38;5;28mdict\u001b[39m[\u001b[38;5;28mstr\u001b[39m, Any], Any]] = []\n\u001b[32m 2717\u001b[39m interrupts: \u001b[38;5;28mlist\u001b[39m[Interrupt] = []\n\u001b[32m-> \u001b[39m\u001b[32m2719\u001b[39m \u001b[43m\u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mstream\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 2720\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 2721\u001b[39m \u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2722\u001b[39m \u001b[43m \u001b[49m\u001b[43mstream_mode\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstream_mode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2723\u001b[39m \u001b[43m \u001b[49m\u001b[43moutput_keys\u001b[49m\u001b[43m=\u001b[49m\u001b[43moutput_keys\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2724\u001b[39m \u001b[43m \u001b[49m\u001b[43minterrupt_before\u001b[49m\u001b[43m=\u001b[49m\u001b[43minterrupt_before\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2725\u001b[39m \u001b[43m \u001b[49m\u001b[43minterrupt_after\u001b[49m\u001b[43m=\u001b[49m\u001b[43minterrupt_after\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2726\u001b[39m \u001b[43m \u001b[49m\u001b[43mcheckpoint_during\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcheckpoint_during\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2727\u001b[39m \u001b[43m \u001b[49m\u001b[43mdebug\u001b[49m\u001b[43m=\u001b[49m\u001b[43mdebug\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2728\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2729\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[32m 2730\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mstream_mode\u001b[49m\u001b[43m \u001b[49m\u001b[43m==\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mvalues\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\n\u001b[32m 2731\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 2732\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43misinstance\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mdict\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 2733\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;129;43;01mand\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mints\u001b[49m\u001b[43m \u001b[49m\u001b[43m:=\u001b[49m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mINTERRUPT\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\n\u001b[32m 2734\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n",
|
| 281 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langgraph\\pregel\\__init__.py:2436\u001b[39m, in \u001b[36mPregel.stream\u001b[39m\u001b[34m(self, input, config, stream_mode, output_keys, interrupt_before, interrupt_after, checkpoint_during, debug, subgraphs)\u001b[39m\n\u001b[32m 2434\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m task \u001b[38;5;129;01min\u001b[39;00m loop.match_cached_writes():\n\u001b[32m 2435\u001b[39m loop.output_writes(task.id, task.writes, cached=\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[32m-> \u001b[39m\u001b[32m2436\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m_\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrunner\u001b[49m\u001b[43m.\u001b[49m\u001b[43mtick\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 2437\u001b[39m \u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43mt\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mloop\u001b[49m\u001b[43m.\u001b[49m\u001b[43mtasks\u001b[49m\u001b[43m.\u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[43m.\u001b[49m\u001b[43mwrites\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2438\u001b[39m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mstep_timeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2439\u001b[39m \u001b[43m \u001b[49m\u001b[43mget_waiter\u001b[49m\u001b[43m=\u001b[49m\u001b[43mget_waiter\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2440\u001b[39m \u001b[43m \u001b[49m\u001b[43mschedule_task\u001b[49m\u001b[43m=\u001b[49m\u001b[43mloop\u001b[49m\u001b[43m.\u001b[49m\u001b[43maccept_push\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2441\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[32m 2442\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# emit output\u001b[39;49;00m\n\u001b[32m 2443\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43;01myield from\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43moutput\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 2444\u001b[39m \u001b[38;5;66;03m# emit output\u001b[39;00m\n",
|
| 282 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langgraph\\pregel\\runner.py:161\u001b[39m, in \u001b[36mPregelRunner.tick\u001b[39m\u001b[34m(self, tasks, reraise, timeout, retry_policy, get_waiter, schedule_task)\u001b[39m\n\u001b[32m 159\u001b[39m t = tasks[\u001b[32m0\u001b[39m]\n\u001b[32m 160\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m161\u001b[39m \u001b[43mrun_with_retry\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 162\u001b[39m \u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 163\u001b[39m \u001b[43m \u001b[49m\u001b[43mretry_policy\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 164\u001b[39m \u001b[43m \u001b[49m\u001b[43mconfigurable\u001b[49m\u001b[43m=\u001b[49m\u001b[43m{\u001b[49m\n\u001b[32m 165\u001b[39m \u001b[43m \u001b[49m\u001b[43mCONFIG_KEY_CALL\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpartial\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 166\u001b[39m \u001b[43m \u001b[49m\u001b[43m_call\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 167\u001b[39m \u001b[43m \u001b[49m\u001b[43mweakref\u001b[49m\u001b[43m.\u001b[49m\u001b[43mref\u001b[49m\u001b[43m(\u001b[49m\u001b[43mt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 168\u001b[39m \u001b[43m \u001b[49m\u001b[43mretry\u001b[49m\u001b[43m=\u001b[49m\u001b[43mretry_policy\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 169\u001b[39m \u001b[43m \u001b[49m\u001b[43mfutures\u001b[49m\u001b[43m=\u001b[49m\u001b[43mweakref\u001b[49m\u001b[43m.\u001b[49m\u001b[43mref\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfutures\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 170\u001b[39m \u001b[43m \u001b[49m\u001b[43mschedule_task\u001b[49m\u001b[43m=\u001b[49m\u001b[43mschedule_task\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 171\u001b[39m \u001b[43m \u001b[49m\u001b[43msubmit\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43msubmit\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 172\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 173\u001b[39m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 174\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 175\u001b[39m \u001b[38;5;28mself\u001b[39m.commit(t, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[32m 176\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n",
|
| 283 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langgraph\\pregel\\retry.py:40\u001b[39m, in \u001b[36mrun_with_retry\u001b[39m\u001b[34m(task, retry_policy, configurable)\u001b[39m\n\u001b[32m 38\u001b[39m task.writes.clear()\n\u001b[32m 39\u001b[39m \u001b[38;5;66;03m# run the task\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m40\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtask\u001b[49m\u001b[43m.\u001b[49m\u001b[43mproc\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtask\u001b[49m\u001b[43m.\u001b[49m\u001b[43minput\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 41\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m ParentCommand \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[32m 42\u001b[39m ns: \u001b[38;5;28mstr\u001b[39m = config[CONF][CONFIG_KEY_CHECKPOINT_NS]\n",
|
| 284 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langgraph\\utils\\runnable.py:623\u001b[39m, in \u001b[36mRunnableSeq.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 621\u001b[39m \u001b[38;5;66;03m# run in context\u001b[39;00m\n\u001b[32m 622\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m set_config_context(config, run) \u001b[38;5;28;01mas\u001b[39;00m context:\n\u001b[32m--> \u001b[39m\u001b[32m623\u001b[39m \u001b[38;5;28minput\u001b[39m = \u001b[43mcontext\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstep\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 624\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 625\u001b[39m \u001b[38;5;28minput\u001b[39m = step.invoke(\u001b[38;5;28minput\u001b[39m, config)\n",
|
| 285 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langgraph\\utils\\runnable.py:377\u001b[39m, in \u001b[36mRunnableCallable.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 375\u001b[39m run_manager.on_chain_end(ret)\n\u001b[32m 376\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m377\u001b[39m ret = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 378\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m.recurse \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(ret, Runnable):\n\u001b[32m 379\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m ret.invoke(\u001b[38;5;28minput\u001b[39m, config)\n",
|
| 286 |
+
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[14]\u001b[39m\u001b[32m, line 27\u001b[39m, in \u001b[36mbuild_graph.<locals>.assistant\u001b[39m\u001b[34m(state)\u001b[39m\n\u001b[32m 25\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34massistant\u001b[39m(state: MessagesState):\n\u001b[32m 26\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"Assistant node\"\"\"\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m27\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m {\u001b[33m\"\u001b[39m\u001b[33mmessages\u001b[39m\u001b[33m\"\u001b[39m: [\u001b[43mllm_with_tools\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstate\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmessages\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m]}\n",
|
| 287 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langchain_core\\runnables\\base.py:5431\u001b[39m, in \u001b[36mRunnableBindingBase.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 5424\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 5425\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34minvoke\u001b[39m(\n\u001b[32m 5426\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 5429\u001b[39m **kwargs: Optional[Any],\n\u001b[32m 5430\u001b[39m ) -> Output:\n\u001b[32m-> \u001b[39m\u001b[32m5431\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mbound\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 5432\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 5433\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_merge_configs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5434\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43m{\u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5435\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
|
| 288 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langchain_core\\language_models\\chat_models.py:372\u001b[39m, in \u001b[36mBaseChatModel.invoke\u001b[39m\u001b[34m(self, input, config, stop, **kwargs)\u001b[39m\n\u001b[32m 360\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 361\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34minvoke\u001b[39m(\n\u001b[32m 362\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 367\u001b[39m **kwargs: Any,\n\u001b[32m 368\u001b[39m ) -> BaseMessage:\n\u001b[32m 369\u001b[39m config = ensure_config(config)\n\u001b[32m 370\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m cast(\n\u001b[32m 371\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mChatGeneration\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m--> \u001b[39m\u001b[32m372\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mgenerate_prompt\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 373\u001b[39m \u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_convert_input\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 374\u001b[39m \u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 375\u001b[39m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mcallbacks\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 376\u001b[39m \u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtags\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 377\u001b[39m \u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmetadata\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 378\u001b[39m \u001b[43m \u001b[49m\u001b[43mrun_name\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mrun_name\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 379\u001b[39m \u001b[43m \u001b[49m\u001b[43mrun_id\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpop\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mrun_id\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 380\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 381\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m.generations[\u001b[32m0\u001b[39m][\u001b[32m0\u001b[39m],\n\u001b[32m 382\u001b[39m ).message\n",
|
| 289 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langchain_core\\language_models\\chat_models.py:957\u001b[39m, in \u001b[36mBaseChatModel.generate_prompt\u001b[39m\u001b[34m(self, prompts, stop, callbacks, **kwargs)\u001b[39m\n\u001b[32m 948\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 949\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mgenerate_prompt\u001b[39m(\n\u001b[32m 950\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 954\u001b[39m **kwargs: Any,\n\u001b[32m 955\u001b[39m ) -> LLMResult:\n\u001b[32m 956\u001b[39m prompt_messages = [p.to_messages() \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;129;01min\u001b[39;00m prompts]\n\u001b[32m--> \u001b[39m\u001b[32m957\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprompt_messages\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 290 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langchain_core\\language_models\\chat_models.py:776\u001b[39m, in \u001b[36mBaseChatModel.generate\u001b[39m\u001b[34m(self, messages, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)\u001b[39m\n\u001b[32m 773\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m i, m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(input_messages):\n\u001b[32m 774\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m 775\u001b[39m results.append(\n\u001b[32m--> \u001b[39m\u001b[32m776\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_generate_with_cache\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 777\u001b[39m \u001b[43m \u001b[49m\u001b[43mm\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 778\u001b[39m \u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 779\u001b[39m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrun_managers\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrun_managers\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 780\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 781\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 782\u001b[39m )\n\u001b[32m 783\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[32m 784\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m run_managers:\n",
|
| 291 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langchain_core\\language_models\\chat_models.py:1022\u001b[39m, in \u001b[36mBaseChatModel._generate_with_cache\u001b[39m\u001b[34m(self, messages, stop, run_manager, **kwargs)\u001b[39m\n\u001b[32m 1020\u001b[39m result = generate_from_stream(\u001b[38;5;28miter\u001b[39m(chunks))\n\u001b[32m 1021\u001b[39m \u001b[38;5;28;01melif\u001b[39;00m inspect.signature(\u001b[38;5;28mself\u001b[39m._generate).parameters.get(\u001b[33m\"\u001b[39m\u001b[33mrun_manager\u001b[39m\u001b[33m\"\u001b[39m):\n\u001b[32m-> \u001b[39m\u001b[32m1022\u001b[39m result = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_generate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 1023\u001b[39m \u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\n\u001b[32m 1024\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 1025\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 1026\u001b[39m result = \u001b[38;5;28mself\u001b[39m._generate(messages, stop=stop, **kwargs)\n",
|
| 292 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\langchain_groq\\chat_models.py:498\u001b[39m, in \u001b[36mChatGroq._generate\u001b[39m\u001b[34m(self, messages, stop, run_manager, **kwargs)\u001b[39m\n\u001b[32m 493\u001b[39m message_dicts, params = \u001b[38;5;28mself\u001b[39m._create_message_dicts(messages, stop)\n\u001b[32m 494\u001b[39m params = {\n\u001b[32m 495\u001b[39m **params,\n\u001b[32m 496\u001b[39m **kwargs,\n\u001b[32m 497\u001b[39m }\n\u001b[32m--> \u001b[39m\u001b[32m498\u001b[39m response = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mclient\u001b[49m\u001b[43m.\u001b[49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmessage_dicts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 499\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m._create_chat_result(response)\n",
|
| 293 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\groq\\resources\\chat\\completions.py:368\u001b[39m, in \u001b[36mCompletions.create\u001b[39m\u001b[34m(self, messages, model, exclude_domains, frequency_penalty, function_call, functions, include_domains, logit_bias, logprobs, max_completion_tokens, max_tokens, metadata, n, parallel_tool_calls, presence_penalty, reasoning_effort, reasoning_format, response_format, search_settings, seed, service_tier, stop, store, stream, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)\u001b[39m\n\u001b[32m 181\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mcreate\u001b[39m(\n\u001b[32m 182\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m 183\u001b[39m *,\n\u001b[32m (...)\u001b[39m\u001b[32m 229\u001b[39m timeout: \u001b[38;5;28mfloat\u001b[39m | httpx.Timeout | \u001b[38;5;28;01mNone\u001b[39;00m | NotGiven = NOT_GIVEN,\n\u001b[32m 230\u001b[39m ) -> ChatCompletion | Stream[ChatCompletionChunk]:\n\u001b[32m 231\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 232\u001b[39m \u001b[33;03m Creates a model response for the given chat conversation.\u001b[39;00m\n\u001b[32m 233\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 366\u001b[39m \u001b[33;03m timeout: Override the client-level default timeout for this request, in seconds\u001b[39;00m\n\u001b[32m 367\u001b[39m \u001b[33;03m \"\"\"\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m368\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_post\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 369\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m/openai/v1/chat/completions\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 370\u001b[39m \u001b[43m \u001b[49m\u001b[43mbody\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmaybe_transform\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 371\u001b[39m \u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[32m 372\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmessages\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 373\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmodel\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 374\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mexclude_domains\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mexclude_domains\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 375\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mfrequency_penalty\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrequency_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 376\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mfunction_call\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunction_call\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 377\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mfunctions\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunctions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 378\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43minclude_domains\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43minclude_domains\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 379\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mlogit_bias\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogit_bias\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 380\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mlogprobs\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 381\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmax_completion_tokens\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_completion_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 382\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmax_tokens\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 383\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmetadata\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 384\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mn\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 385\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mparallel_tool_calls\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mparallel_tool_calls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 386\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mpresence_penalty\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpresence_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 387\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mreasoning_effort\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mreasoning_effort\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 388\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mreasoning_format\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mreasoning_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 389\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mresponse_format\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 390\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43msearch_settings\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43msearch_settings\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 391\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mseed\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mseed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 392\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mservice_tier\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mservice_tier\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 393\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mstop\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 394\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mstore\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstore\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 395\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mstream\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 396\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtemperature\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemperature\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 397\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtool_choice\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtool_choice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 398\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtools\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtools\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 399\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtop_logprobs\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_logprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 400\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtop_p\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_p\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 401\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43muser\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43muser\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 402\u001b[39m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 403\u001b[39m \u001b[43m \u001b[49m\u001b[43mcompletion_create_params\u001b[49m\u001b[43m.\u001b[49m\u001b[43mCompletionCreateParams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 404\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 405\u001b[39m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmake_request_options\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 406\u001b[39m \u001b[43m \u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[43m=\u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_query\u001b[49m\u001b[43m=\u001b[49m\u001b[43mextra_query\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_body\u001b[49m\u001b[43m=\u001b[49m\u001b[43mextra_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m=\u001b[49m\u001b[43mtimeout\u001b[49m\n\u001b[32m 407\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 408\u001b[39m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m=\u001b[49m\u001b[43mChatCompletion\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 409\u001b[39m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstream\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 410\u001b[39m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[43m=\u001b[49m\u001b[43mStream\u001b[49m\u001b[43m[\u001b[49m\u001b[43mChatCompletionChunk\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 411\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
|
| 294 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\groq\\_base_client.py:1225\u001b[39m, in \u001b[36mSyncAPIClient.post\u001b[39m\u001b[34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[39m\n\u001b[32m 1211\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mpost\u001b[39m(\n\u001b[32m 1212\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m 1213\u001b[39m path: \u001b[38;5;28mstr\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 1220\u001b[39m stream_cls: \u001b[38;5;28mtype\u001b[39m[_StreamT] | \u001b[38;5;28;01mNone\u001b[39;00m = \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m 1221\u001b[39m ) -> ResponseT | _StreamT:\n\u001b[32m 1222\u001b[39m opts = FinalRequestOptions.construct(\n\u001b[32m 1223\u001b[39m method=\u001b[33m\"\u001b[39m\u001b[33mpost\u001b[39m\u001b[33m\"\u001b[39m, url=path, json_data=body, files=to_httpx_files(files), **options\n\u001b[32m 1224\u001b[39m )\n\u001b[32m-> \u001b[39m\u001b[32m1225\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m cast(ResponseT, \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mopts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[43m)\u001b[49m)\n",
|
| 295 |
+
"\u001b[36mFile \u001b[39m\u001b[32md:\\coding\\github-mine\\agents\\huggingface_course\\Final_Assignment_Template\\.venv\\Lib\\site-packages\\groq\\_base_client.py:1034\u001b[39m, in \u001b[36mSyncAPIClient.request\u001b[39m\u001b[34m(self, cast_to, options, stream, stream_cls)\u001b[39m\n\u001b[32m 1031\u001b[39m err.response.read()\n\u001b[32m 1033\u001b[39m log.debug(\u001b[33m\"\u001b[39m\u001b[33mRe-raising status error\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m-> \u001b[39m\u001b[32m1034\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m._make_status_error_from_response(err.response) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 1036\u001b[39m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[32m 1038\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m response \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[33m\"\u001b[39m\u001b[33mcould not resolve response (should never happen)\u001b[39m\u001b[33m\"\u001b[39m\n",
|
| 296 |
+
"\u001b[31mPermissionDeniedError\u001b[39m: Error code: 403 - {'error': {'message': 'Access denied. Please check your network settings.'}}",
|
| 297 |
+
"During task with name 'assistant' and id 'ea13c7cc-564f-cc7e-d2d0-497d7d7645ef'"
|
| 298 |
+
]
|
| 299 |
+
}
|
| 300 |
+
],
|
| 301 |
+
"source": [
|
| 302 |
+
"question = \"When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?\"\n",
|
| 303 |
+
"# Build the graph\n",
|
| 304 |
+
"graph = build_graph(provider=\"groq\")\n",
|
| 305 |
+
"# Run the graph\n",
|
| 306 |
+
"messages = [HumanMessage(content=question)]\n",
|
| 307 |
+
"messages = graph.invoke({\"messages\": messages})\n",
|
| 308 |
+
"for m in messages[\"messages\"]:\n",
|
| 309 |
+
" m.pretty_print()"
|
| 310 |
+
]
|
| 311 |
+
},
|
| 312 |
+
{
|
| 313 |
+
"cell_type": "code",
|
| 314 |
+
"execution_count": null,
|
| 315 |
+
"metadata": {},
|
| 316 |
+
"outputs": [],
|
| 317 |
+
"source": []
|
| 318 |
+
}
|
| 319 |
+
],
|
| 320 |
+
"metadata": {
|
| 321 |
+
"kernelspec": {
|
| 322 |
+
"display_name": ".venv",
|
| 323 |
+
"language": "python",
|
| 324 |
+
"name": "python3"
|
| 325 |
+
},
|
| 326 |
+
"language_info": {
|
| 327 |
+
"codemirror_mode": {
|
| 328 |
+
"name": "ipython",
|
| 329 |
+
"version": 3
|
| 330 |
+
},
|
| 331 |
+
"file_extension": ".py",
|
| 332 |
+
"mimetype": "text/x-python",
|
| 333 |
+
"name": "python",
|
| 334 |
+
"nbconvert_exporter": "python",
|
| 335 |
+
"pygments_lexer": "ipython3",
|
| 336 |
+
"version": "3.13.3"
|
| 337 |
+
}
|
| 338 |
+
},
|
| 339 |
+
"nbformat": 4,
|
| 340 |
+
"nbformat_minor": 2
|
| 341 |
+
}
|
app.py
CHANGED
|
@@ -3,7 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from agents import LlamaIndexAgent
|
|
|
|
| 7 |
import asyncio
|
| 8 |
import aiohttp
|
| 9 |
|
|
@@ -15,7 +16,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
-
self.agent =
|
| 19 |
print("BasicAgent initialized.")
|
| 20 |
async def aquery(self, question: str) -> str:
|
| 21 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
# from agents import LlamaIndexAgent
|
| 7 |
+
from langraph_agent import build_graph
|
| 8 |
import asyncio
|
| 9 |
import aiohttp
|
| 10 |
|
|
|
|
| 16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
+
self.agent = build_graph()
|
| 20 |
print("BasicAgent initialized.")
|
| 21 |
async def aquery(self, question: str) -> str:
|
| 22 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
chat_models_check.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
|
| 3 |
+
api = HfApi()
|
| 4 |
+
chat_models = api.list_models(filter="pipeline_tag:chat-completion")
|
| 5 |
+
print([m.modelId for m in chat_models])
|
langraph_agent.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""LangGraph Agent"""
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 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 |
+
from langchain_core.tools import tool
|
| 16 |
+
from langchain.tools.retriever import create_retriever_tool
|
| 17 |
+
from supabase.client import Client, create_client
|
| 18 |
+
|
| 19 |
+
load_dotenv()
|
| 20 |
+
|
| 21 |
+
@tool
|
| 22 |
+
def multiply(a: int, b: int) -> int:
|
| 23 |
+
"""Multiply two numbers.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
a: first int
|
| 27 |
+
b: second int
|
| 28 |
+
"""
|
| 29 |
+
return a * b
|
| 30 |
+
|
| 31 |
+
@tool
|
| 32 |
+
def add(a: int, b: int) -> int:
|
| 33 |
+
"""Add two numbers.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
a: first int
|
| 37 |
+
b: second int
|
| 38 |
+
"""
|
| 39 |
+
return a + b
|
| 40 |
+
|
| 41 |
+
@tool
|
| 42 |
+
def subtract(a: int, b: int) -> int:
|
| 43 |
+
"""Subtract two numbers.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
a: first int
|
| 47 |
+
b: second int
|
| 48 |
+
"""
|
| 49 |
+
return a - b
|
| 50 |
+
|
| 51 |
+
@tool
|
| 52 |
+
def divide(a: int, b: int) -> int:
|
| 53 |
+
"""Divide two numbers.
|
| 54 |
+
|
| 55 |
+
Args:
|
| 56 |
+
a: first int
|
| 57 |
+
b: second int
|
| 58 |
+
"""
|
| 59 |
+
if b == 0:
|
| 60 |
+
raise ValueError("Cannot divide by zero.")
|
| 61 |
+
return a / b
|
| 62 |
+
|
| 63 |
+
@tool
|
| 64 |
+
def modulus(a: int, b: int) -> int:
|
| 65 |
+
"""Get the modulus of two numbers.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
a: first int
|
| 69 |
+
b: second int
|
| 70 |
+
"""
|
| 71 |
+
return a % b
|
| 72 |
+
|
| 73 |
+
@tool
|
| 74 |
+
def wiki_search(query: str) -> str:
|
| 75 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
query: The search query."""
|
| 79 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 80 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 81 |
+
[
|
| 82 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 83 |
+
for doc in search_docs
|
| 84 |
+
])
|
| 85 |
+
return {"wiki_results": formatted_search_docs}
|
| 86 |
+
|
| 87 |
+
@tool
|
| 88 |
+
def web_search(query: str) -> str:
|
| 89 |
+
"""Search Tavily for a query and return maximum 3 results.
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
query: The search query."""
|
| 93 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 94 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 95 |
+
[
|
| 96 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 97 |
+
for doc in search_docs
|
| 98 |
+
])
|
| 99 |
+
return {"web_results": formatted_search_docs}
|
| 100 |
+
|
| 101 |
+
@tool
|
| 102 |
+
def arvix_search(query: str) -> str:
|
| 103 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 104 |
+
|
| 105 |
+
Args:
|
| 106 |
+
query: The search query."""
|
| 107 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 108 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 109 |
+
[
|
| 110 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 111 |
+
for doc in search_docs
|
| 112 |
+
])
|
| 113 |
+
return {"arvix_results": formatted_search_docs}
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# load the system prompt from the file
|
| 118 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 119 |
+
system_prompt = f.read()
|
| 120 |
+
|
| 121 |
+
# System message
|
| 122 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 123 |
+
|
| 124 |
+
# build a retriever
|
| 125 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") # dim=768
|
| 126 |
+
supabase: Client = create_client(
|
| 127 |
+
os.environ.get("SUPABASE_URL"),
|
| 128 |
+
os.environ.get("SUPABASE_SERVICE_KEY"))
|
| 129 |
+
vector_store = SupabaseVectorStore(
|
| 130 |
+
client=supabase,
|
| 131 |
+
embedding= embeddings,
|
| 132 |
+
table_name="documents",
|
| 133 |
+
query_name="match_documents_langchain",
|
| 134 |
+
)
|
| 135 |
+
create_retriever_tool = create_retriever_tool(
|
| 136 |
+
retriever=vector_store.as_retriever(),
|
| 137 |
+
name="Question Search",
|
| 138 |
+
description="A tool to retrieve similar questions from a vector store.",
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
tools = [
|
| 144 |
+
multiply,
|
| 145 |
+
add,
|
| 146 |
+
subtract,
|
| 147 |
+
divide,
|
| 148 |
+
modulus,
|
| 149 |
+
wiki_search,
|
| 150 |
+
web_search,
|
| 151 |
+
arvix_search,
|
| 152 |
+
]
|
| 153 |
+
|
| 154 |
+
# Build graph function
|
| 155 |
+
def build_graph(provider: str = "groq"):
|
| 156 |
+
"""Build the graph"""
|
| 157 |
+
# Load environment variables from .env file
|
| 158 |
+
if provider == "google":
|
| 159 |
+
# Google Gemini
|
| 160 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 161 |
+
elif provider == "groq":
|
| 162 |
+
# Groq https://console.groq.com/docs/models
|
| 163 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0) # optional : qwen-qwq-32b gemma2-9b-it
|
| 164 |
+
elif provider == "huggingface":
|
| 165 |
+
# TODO: Add huggingface endpoint
|
| 166 |
+
llm = ChatHuggingFace(
|
| 167 |
+
llm=HuggingFaceEndpoint(
|
| 168 |
+
url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
|
| 169 |
+
temperature=0,
|
| 170 |
+
),
|
| 171 |
+
)
|
| 172 |
+
else:
|
| 173 |
+
raise ValueError("Invalid provider. Choose 'google', 'groq' or 'huggingface'.")
|
| 174 |
+
# Bind tools to LLM
|
| 175 |
+
llm_with_tools = llm.bind_tools(tools)
|
| 176 |
+
|
| 177 |
+
# Node
|
| 178 |
+
def assistant(state: MessagesState):
|
| 179 |
+
"""Assistant node"""
|
| 180 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 181 |
+
|
| 182 |
+
def retriever(state: MessagesState):
|
| 183 |
+
"""Retriever node"""
|
| 184 |
+
similar_question = vector_store.similarity_search(state["messages"][0].content)
|
| 185 |
+
example_msg = HumanMessage(
|
| 186 |
+
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
| 187 |
+
)
|
| 188 |
+
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
| 189 |
+
|
| 190 |
+
builder = StateGraph(MessagesState)
|
| 191 |
+
builder.add_node("retriever", retriever)
|
| 192 |
+
builder.add_node("assistant", assistant)
|
| 193 |
+
builder.add_node("tools", ToolNode(tools))
|
| 194 |
+
builder.add_edge(START, "retriever")
|
| 195 |
+
builder.add_edge("retriever", "assistant")
|
| 196 |
+
builder.add_conditional_edges(
|
| 197 |
+
"assistant",
|
| 198 |
+
tools_condition,
|
| 199 |
+
)
|
| 200 |
+
builder.add_edge("tools", "assistant")
|
| 201 |
+
|
| 202 |
+
# Compile graph
|
| 203 |
+
return builder.compile()
|
| 204 |
+
|
| 205 |
+
# test
|
| 206 |
+
if __name__ == "__main__":
|
| 207 |
+
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 208 |
+
# Build the graph
|
| 209 |
+
graph = build_graph(provider="groq")
|
| 210 |
+
# Run the graph
|
| 211 |
+
messages = [HumanMessage(content=question)]
|
| 212 |
+
messages = graph.invoke({"messages": messages})
|
| 213 |
+
for m in messages["messages"]:
|
| 214 |
+
m.pretty_print()
|
pyproject.toml
CHANGED
|
@@ -6,8 +6,17 @@ readme = "README.md"
|
|
| 6 |
requires-python = ">=3.13"
|
| 7 |
dependencies = [
|
| 8 |
"dotenv>=0.9.9",
|
| 9 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"langfuse>=3.0.0",
|
|
|
|
| 11 |
"llama-index>=0.12.40",
|
| 12 |
"llama-index-core>=0.12.40",
|
| 13 |
"llama-index-llms-huggingface-api>=0.5.0",
|
|
@@ -16,5 +25,7 @@ dependencies = [
|
|
| 16 |
"llama-index-tools-duckduckgo>=0.3.0",
|
| 17 |
"llama-index-tools-tavily-research>=0.3.0",
|
| 18 |
"rich>=14.0.0",
|
|
|
|
|
|
|
| 19 |
"wikipedia>=1.4.0",
|
| 20 |
]
|
|
|
|
| 6 |
requires-python = ">=3.13"
|
| 7 |
dependencies = [
|
| 8 |
"dotenv>=0.9.9",
|
| 9 |
+
"hf-xet>=1.1.3",
|
| 10 |
+
"huggingface-hub[hf-xet]>=0.32.4",
|
| 11 |
+
"ipykernel>=6.29.5",
|
| 12 |
+
"ipywidgets>=8.1.7",
|
| 13 |
+
"langchain>=0.3.25",
|
| 14 |
+
"langchain-community>=0.3.25",
|
| 15 |
+
"langchain-google-genai>=2.1.5",
|
| 16 |
+
"langchain-groq>=0.3.2",
|
| 17 |
+
"langchain-huggingface>=0.3.0",
|
| 18 |
"langfuse>=3.0.0",
|
| 19 |
+
"langgraph>=0.4.8",
|
| 20 |
"llama-index>=0.12.40",
|
| 21 |
"llama-index-core>=0.12.40",
|
| 22 |
"llama-index-llms-huggingface-api>=0.5.0",
|
|
|
|
| 25 |
"llama-index-tools-duckduckgo>=0.3.0",
|
| 26 |
"llama-index-tools-tavily-research>=0.3.0",
|
| 27 |
"rich>=14.0.0",
|
| 28 |
+
"sentence-transformers>=4.1.0",
|
| 29 |
+
"supabase>=2.15.3",
|
| 30 |
"wikipedia>=1.4.0",
|
| 31 |
]
|
test.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" Basic Agent Evaluation Runner"""
|
| 2 |
+
import os
|
| 3 |
+
import inspect
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
+
from agent import build_graph
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# (Keep Constants as is)
|
| 13 |
+
# --- Constants ---
|
| 14 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
+
|
| 16 |
+
# --- Basic Agent Definition ---
|
| 17 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class BasicAgent:
|
| 21 |
+
"""A langgraph agent."""
|
| 22 |
+
def __init__(self):
|
| 23 |
+
print("BasicAgent initialized.")
|
| 24 |
+
self.graph = build_graph()
|
| 25 |
+
|
| 26 |
+
def __call__(self, question: str) -> str:
|
| 27 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 28 |
+
# Wrap the question in a HumanMessage from langchain_core
|
| 29 |
+
messages = [HumanMessage(content=question)]
|
| 30 |
+
messages = self.graph.invoke({"messages": messages})
|
| 31 |
+
answer = messages['messages'][-1].content
|
| 32 |
+
return answer[14:]
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 36 |
+
"""
|
| 37 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 38 |
+
and displays the results.
|
| 39 |
+
"""
|
| 40 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 41 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 42 |
+
|
| 43 |
+
if profile:
|
| 44 |
+
username= f"{profile.username}"
|
| 45 |
+
print(f"User logged in: {username}")
|
| 46 |
+
else:
|
| 47 |
+
print("User not logged in.")
|
| 48 |
+
return "Please Login to Hugging Face with the button.", None
|
| 49 |
+
|
| 50 |
+
api_url = DEFAULT_API_URL
|
| 51 |
+
questions_url = f"{api_url}/questions"
|
| 52 |
+
submit_url = f"{api_url}/submit"
|
| 53 |
+
|
| 54 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 55 |
+
try:
|
| 56 |
+
agent = BasicAgent()
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Error instantiating agent: {e}")
|
| 59 |
+
return f"Error initializing agent: {e}", None
|
| 60 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 61 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 62 |
+
print(agent_code)
|
| 63 |
+
|
| 64 |
+
# 2. Fetch Questions
|
| 65 |
+
print(f"Fetching questions from: {questions_url}")
|
| 66 |
+
try:
|
| 67 |
+
response = requests.get(questions_url, timeout=15)
|
| 68 |
+
response.raise_for_status()
|
| 69 |
+
questions_data = response.json()
|
| 70 |
+
if not questions_data:
|
| 71 |
+
print("Fetched questions list is empty.")
|
| 72 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 73 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 74 |
+
except requests.exceptions.RequestException as e:
|
| 75 |
+
print(f"Error fetching questions: {e}")
|
| 76 |
+
return f"Error fetching questions: {e}", None
|
| 77 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 78 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 79 |
+
print(f"Response text: {response.text[:500]}")
|
| 80 |
+
return f"Error decoding server response for questions: {e}", None
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 83 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
| 84 |
+
|
| 85 |
+
# 3. Run your Agent
|
| 86 |
+
results_log = []
|
| 87 |
+
answers_payload = []
|
| 88 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 89 |
+
for item in questions_data:
|
| 90 |
+
task_id = item.get("task_id")
|
| 91 |
+
question_text = item.get("question")
|
| 92 |
+
if not task_id or question_text is None:
|
| 93 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 94 |
+
continue
|
| 95 |
+
try:
|
| 96 |
+
submitted_answer = agent(question_text)
|
| 97 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 98 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 101 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 102 |
+
|
| 103 |
+
if not answers_payload:
|
| 104 |
+
print("Agent did not produce any answers to submit.")
|
| 105 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 106 |
+
|
| 107 |
+
# 4. Prepare Submission
|
| 108 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 109 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 110 |
+
print(status_update)
|
| 111 |
+
|
| 112 |
+
# 5. Submit
|
| 113 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 114 |
+
try:
|
| 115 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 116 |
+
response.raise_for_status()
|
| 117 |
+
result_data = response.json()
|
| 118 |
+
final_status = (
|
| 119 |
+
f"Submission Successful!\n"
|
| 120 |
+
f"User: {result_data.get('username')}\n"
|
| 121 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 122 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 123 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 124 |
+
)
|
| 125 |
+
print("Submission successful.")
|
| 126 |
+
results_df = pd.DataFrame(results_log)
|
| 127 |
+
return final_status, results_df
|
| 128 |
+
except requests.exceptions.HTTPError as e:
|
| 129 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 130 |
+
try:
|
| 131 |
+
error_json = e.response.json()
|
| 132 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 133 |
+
except requests.exceptions.JSONDecodeError:
|
| 134 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 135 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 136 |
+
print(status_message)
|
| 137 |
+
results_df = pd.DataFrame(results_log)
|
| 138 |
+
return status_message, results_df
|
| 139 |
+
except requests.exceptions.Timeout:
|
| 140 |
+
status_message = "Submission Failed: The request timed out."
|
| 141 |
+
print(status_message)
|
| 142 |
+
results_df = pd.DataFrame(results_log)
|
| 143 |
+
return status_message, results_df
|
| 144 |
+
except requests.exceptions.RequestException as e:
|
| 145 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 146 |
+
print(status_message)
|
| 147 |
+
results_df = pd.DataFrame(results_log)
|
| 148 |
+
return status_message, results_df
|
| 149 |
+
except Exception as e:
|
| 150 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 151 |
+
print(status_message)
|
| 152 |
+
results_df = pd.DataFrame(results_log)
|
| 153 |
+
return status_message, results_df
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# --- Build Gradio Interface using Blocks ---
|
| 157 |
+
with gr.Blocks() as demo:
|
| 158 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 159 |
+
gr.Markdown(
|
| 160 |
+
"""
|
| 161 |
+
**Instructions:**
|
| 162 |
+
|
| 163 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 164 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 165 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 166 |
+
|
| 167 |
+
---
|
| 168 |
+
**Disclaimers:**
|
| 169 |
+
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 170 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 171 |
+
"""
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
gr.LoginButton()
|
| 175 |
+
|
| 176 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 177 |
+
|
| 178 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 179 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 180 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 181 |
+
|
| 182 |
+
run_button.click(
|
| 183 |
+
fn=run_and_submit_all,
|
| 184 |
+
outputs=[status_output, results_table]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
if __name__ == "__main__":
|
| 188 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 189 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 190 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 191 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 192 |
+
|
| 193 |
+
if space_host_startup:
|
| 194 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 195 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 196 |
+
else:
|
| 197 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 198 |
+
|
| 199 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 200 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 201 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 202 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 203 |
+
else:
|
| 204 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 205 |
+
|
| 206 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 207 |
+
|
| 208 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 209 |
+
demo.launch(debug=True, share=False)
|
uv.lock
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|