mohamedsaeed_microsoft commited on
Commit ·
75a4a6b
1
Parent(s): 39dd6b8
Fixed
Browse files- .gitignore +15 -0
- agent.py +190 -214
- app.py +1 -7
- supabase_docs.csv +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python gitignore
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*.class
|
| 5 |
+
*.so
|
| 6 |
+
.env
|
| 7 |
+
.venv
|
| 8 |
+
env/
|
| 9 |
+
venv/
|
| 10 |
+
ENV/
|
| 11 |
+
.idea/
|
| 12 |
+
.vscode/
|
| 13 |
+
*.log
|
| 14 |
+
.DS_Store
|
| 15 |
+
.env
|
agent.py
CHANGED
|
@@ -1,214 +1,190 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
from
|
| 11 |
-
from
|
| 12 |
-
from
|
| 13 |
-
from
|
| 14 |
-
from
|
| 15 |
-
from
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 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()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from langchain.tools import tool
|
| 4 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 5 |
+
from langchain_core.tools import tool
|
| 6 |
+
from langchain.tools.retriever import create_retriever_tool
|
| 7 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 8 |
+
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 9 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 10 |
+
from langchain_groq import ChatGroq
|
| 11 |
+
from langgraph.prebuilt import tools_condition, ToolNode
|
| 12 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 13 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 14 |
+
from langchain_huggingface import ChatHuggingFace
|
| 15 |
+
from supabase.client import Client, create_client
|
| 16 |
+
|
| 17 |
+
load_dotenv()
|
| 18 |
+
|
| 19 |
+
@tool
|
| 20 |
+
def multiply(a: int, b: int) -> int:
|
| 21 |
+
"""Multiply two numbers.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
a: first int
|
| 25 |
+
b: second int
|
| 26 |
+
"""
|
| 27 |
+
return a * b
|
| 28 |
+
|
| 29 |
+
@tool
|
| 30 |
+
def add(a: int, b: int) -> int:
|
| 31 |
+
"""Add two numbers.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
a: first int
|
| 35 |
+
b: second int
|
| 36 |
+
"""
|
| 37 |
+
return a + b
|
| 38 |
+
|
| 39 |
+
@tool
|
| 40 |
+
def subtract(a: int, b: int) -> int:
|
| 41 |
+
"""Subtract two numbers.
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
a: first int
|
| 45 |
+
b: second int
|
| 46 |
+
"""
|
| 47 |
+
return a - b
|
| 48 |
+
|
| 49 |
+
@tool
|
| 50 |
+
def divide(a: int, b: int) -> int:
|
| 51 |
+
"""Divide two numbers.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
a: first int
|
| 55 |
+
b: second int
|
| 56 |
+
"""
|
| 57 |
+
if b == 0:
|
| 58 |
+
raise ValueError("Cannot divide by zero.")
|
| 59 |
+
return a / b
|
| 60 |
+
|
| 61 |
+
@tool
|
| 62 |
+
def modulus(a: int, b: int) -> int:
|
| 63 |
+
"""Get the modulus of two numbers.
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
a: first int
|
| 67 |
+
b: second int
|
| 68 |
+
"""
|
| 69 |
+
return a % b
|
| 70 |
+
|
| 71 |
+
@tool
|
| 72 |
+
def wiki_search(query: str) -> str:
|
| 73 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
| 74 |
+
|
| 75 |
+
Args:
|
| 76 |
+
query: The search query."""
|
| 77 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 78 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 79 |
+
[
|
| 80 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 81 |
+
for doc in search_docs
|
| 82 |
+
])
|
| 83 |
+
return {"wiki_results": formatted_search_docs}
|
| 84 |
+
|
| 85 |
+
@tool
|
| 86 |
+
def web_search(query: str) -> str:
|
| 87 |
+
"""Search Tavily for a query and return maximum 3 results.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
query: The search query."""
|
| 91 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 92 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 93 |
+
[
|
| 94 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 95 |
+
for doc in search_docs
|
| 96 |
+
])
|
| 97 |
+
return {"web_results": formatted_search_docs}
|
| 98 |
+
|
| 99 |
+
@tool
|
| 100 |
+
def arvix_search(query: str) -> str:
|
| 101 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 102 |
+
|
| 103 |
+
Args:
|
| 104 |
+
query: The search query."""
|
| 105 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 106 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 107 |
+
[
|
| 108 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 109 |
+
for doc in search_docs
|
| 110 |
+
])
|
| 111 |
+
return {"arvix_results": formatted_search_docs}
|
| 112 |
+
|
| 113 |
+
# load the system prompt from the file
|
| 114 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 115 |
+
system_prompt = f.read()
|
| 116 |
+
|
| 117 |
+
# System message
|
| 118 |
+
sys_msg = SystemMessage(content=system_prompt)
|
| 119 |
+
|
| 120 |
+
# build a retriever
|
| 121 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") # dim=768
|
| 122 |
+
supabase: Client = create_client(
|
| 123 |
+
os.environ.get("SUPABASE_URL"),
|
| 124 |
+
os.environ.get("SUPABASE_SERVICE_KEY"))
|
| 125 |
+
vector_store = SupabaseVectorStore(
|
| 126 |
+
client=supabase,
|
| 127 |
+
embedding= embeddings,
|
| 128 |
+
table_name="documents",
|
| 129 |
+
query_name="get_docs",
|
| 130 |
+
)
|
| 131 |
+
create_retriever_tool = create_retriever_tool(
|
| 132 |
+
retriever=vector_store.as_retriever(),
|
| 133 |
+
name="Question Search",
|
| 134 |
+
description="A tool to retrieve similar questions from a vector store.",
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
tools = [
|
| 139 |
+
multiply,
|
| 140 |
+
add,
|
| 141 |
+
subtract,
|
| 142 |
+
divide,
|
| 143 |
+
modulus,
|
| 144 |
+
wiki_search,
|
| 145 |
+
web_search,
|
| 146 |
+
arvix_search,
|
| 147 |
+
]
|
| 148 |
+
|
| 149 |
+
# Build the state graph
|
| 150 |
+
def build_graph():
|
| 151 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0)
|
| 152 |
+
llm_with_tools = llm.bind_tools(tools)
|
| 153 |
+
|
| 154 |
+
def assistant_node(state: MessagesState):
|
| 155 |
+
"""Assistant node"""
|
| 156 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 157 |
+
|
| 158 |
+
def retriever_node(state: MessagesState):
|
| 159 |
+
"""Retriever node"""
|
| 160 |
+
similar_question = vector_store.similarity_search(state["messages"][0].content)
|
| 161 |
+
if not similar_question:
|
| 162 |
+
return {"messages": [HumanMessage(content="No similar questions found in the database.")]}
|
| 163 |
+
example_msg = HumanMessage(
|
| 164 |
+
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
| 165 |
+
)
|
| 166 |
+
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
| 167 |
+
|
| 168 |
+
build_graph = StateGraph(MessagesState)
|
| 169 |
+
build_graph.add_node("retreiver", retriever_node)
|
| 170 |
+
build_graph.add_node("assistant", assistant_node)
|
| 171 |
+
build_graph.add_node("tools", ToolNode(tools=tools))
|
| 172 |
+
build_graph.add_edge(START, "retreiver")
|
| 173 |
+
build_graph.add_edge("retreiver", "assistant")
|
| 174 |
+
build_graph.add_conditional_edges(
|
| 175 |
+
"assistant",
|
| 176 |
+
tools_condition
|
| 177 |
+
)
|
| 178 |
+
build_graph.add_edge("tools", "assistant")
|
| 179 |
+
return build_graph.compile()
|
| 180 |
+
|
| 181 |
+
# test
|
| 182 |
+
if __name__ == "__main__":
|
| 183 |
+
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
| 184 |
+
# Build the graph
|
| 185 |
+
graph = build_graph()
|
| 186 |
+
# Run the graph
|
| 187 |
+
messages = [HumanMessage(content=question)]
|
| 188 |
+
messages = graph.invoke({"messages": messages})
|
| 189 |
+
for m in messages["messages"]:
|
| 190 |
+
m.pretty_print()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,22 +1,17 @@
|
|
| 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):
|
|
@@ -31,7 +26,6 @@ class BasicAgent:
|
|
| 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,
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from langchain_core.messages import HumanMessage
|
| 7 |
from agent import build_graph
|
| 8 |
|
|
|
|
|
|
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
| 15 |
class BasicAgent:
|
| 16 |
"""A langgraph agent."""
|
| 17 |
def __init__(self):
|
|
|
|
| 26 |
answer = messages['messages'][-1].content
|
| 27 |
return answer[14:]
|
| 28 |
|
|
|
|
| 29 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 30 |
"""
|
| 31 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
supabase_docs.csv
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|