AnkamRishitha commited on
Commit
0153054
·
verified ·
1 Parent(s): a08aef2

Upload 4 files

Browse files
Files changed (4) hide show
  1. agent.py +210 -0
  2. app.py +205 -0
  3. metadata.jsonl +0 -0
  4. requirements.txt +19 -0
agent.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from langgraph.graph import START, StateGraph, MessagesState
4
+ from langgraph.prebuilt import tools_condition, ToolNode
5
+ from langchain_google_genai import ChatGoogleGenerativeAI
6
+ from langchain_groq import ChatGroq
7
+ from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
8
+ from langchain_community.tools.tavily_search import TavilySearchResults
9
+ from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
10
+ from langchain_community.vectorstores import Chroma
11
+ from langchain_core.documents import Document
12
+ from langchain_core.messages import SystemMessage, HumanMessage
13
+ from langchain_core.tools import tool
14
+ from langchain.tools.retriever import create_retriever_tool
15
+ import json
16
+ from langchain.vectorstores import Chroma
17
+ from langchain.embeddings import HuggingFaceEmbeddings
18
+ from langchain.schema import Document
19
+
20
+ load_dotenv()
21
+
22
+ os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
23
+ groq_api_key = os.getenv("GROQ_API_KEY")
24
+
25
+ # Tools
26
+ @tool
27
+ def multiply(a: int, b: int) -> int:
28
+ """Multiply two numbers.
29
+ Args:
30
+ a: first int
31
+ b: second int
32
+ """
33
+ return a * b
34
+
35
+ @tool
36
+ def add(a: int, b: int) -> int:
37
+ """Add two numbers.
38
+
39
+ Args:
40
+ a: first int
41
+ b: second int
42
+ """
43
+ return a + b
44
+
45
+ @tool
46
+ def subtract(a: int, b: int) -> int:
47
+ """Subtract two numbers.
48
+
49
+ Args:
50
+ a: first int
51
+ b: second int
52
+ """
53
+ return a - b
54
+
55
+ @tool
56
+ def divide(a: int, b: int) -> int:
57
+ """Divide two numbers.
58
+
59
+ Args:
60
+ a: first int
61
+ b: second int
62
+ """
63
+ if b == 0:
64
+ raise ValueError("Cannot divide by zero.")
65
+ return a / b
66
+
67
+ @tool
68
+ def modulus(a: int, b: int) -> int:
69
+ """Get the modulus of two numbers.
70
+
71
+ Args:
72
+ a: first int
73
+ b: second int
74
+ """
75
+ return a % b
76
+
77
+ @tool
78
+ def wiki_search(query: str) -> str:
79
+ """Search Wikipedia for a query and return maximum 2 results.
80
+
81
+ Args:
82
+ query: The search query."""
83
+ search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
84
+ formatted_search_docs = "\n\n---\n\n".join(
85
+ [
86
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
87
+ for doc in search_docs
88
+ ])
89
+ return {"wiki_results": formatted_search_docs}
90
+
91
+ @tool
92
+ def web_search(query: str) -> str:
93
+ """Search Tavily for a query and return maximum 3 results.
94
+
95
+ Args:
96
+ query: The search query."""
97
+ search_docs = TavilySearchResults(max_results=3).invoke(query=query)
98
+ formatted_search_docs = "\n\n---\n\n".join(
99
+ [
100
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
101
+ for doc in search_docs
102
+ ])
103
+ return {"web_results": formatted_search_docs}
104
+
105
+ @tool
106
+ def arvix_search(query: str) -> str:
107
+ """Search Arxiv for a query and return maximum 3 result.
108
+
109
+ Args:
110
+ query: The search query."""
111
+ search_docs = ArxivLoader(query=query, load_max_docs=3).load()
112
+ formatted_search_docs = "\n\n---\n\n".join(
113
+ [
114
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
115
+ for doc in search_docs
116
+ ])
117
+ return {"arvix_results": formatted_search_docs}
118
+
119
+ @tool
120
+ def similar_question_search(question: str) -> str:
121
+ """Search the vector database for similar questions and return the first results.
122
+
123
+ Args:
124
+ question: the question human provided."""
125
+ matched_docs = vector_store.similarity_search(query, 3)
126
+ formatted_search_docs = "\n\n---\n\n".join(
127
+ [
128
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
129
+ for doc in matched_docs
130
+ ])
131
+ return {"similar_questions": formatted_search_docs}
132
+
133
+ # Load system prompt
134
+ system_prompt = """
135
+ You are a helpful assistant tasked with answering questions using a set of tools.
136
+ Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
137
+ FINAL ANSWER: [YOUR FINAL ANSWER].
138
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
139
+ Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
140
+ """
141
+
142
+ # System message
143
+ sys_msg = SystemMessage(content=system_prompt)
144
+
145
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
146
+
147
+ with open('metadata.jsonl', 'r') as jsonl_file:
148
+ json_list = list(jsonl_file)
149
+
150
+ json_QA = []
151
+ for json_str in json_list:
152
+ json_data = json.loads(json_str)
153
+ json_QA.append(json_data)
154
+
155
+ documents = []
156
+ for sample in json_QA:
157
+ content = f"Question : {sample['Question']}\n\nFinal answer : {sample['Final answer']}"
158
+ metadata = {"source": sample["task_id"]}
159
+ documents.append(Document(page_content=content, metadata=metadata))
160
+
161
+ # Initialize vector store and add documents
162
+ vector_store = Chroma.from_documents(
163
+ documents=documents,
164
+ embedding=embeddings,
165
+ persist_directory="./chroma_db",
166
+ collection_name="my_collection"
167
+ )
168
+ vector_store.persist()
169
+ print("Documents inserted:", vector_store._collection.count())
170
+
171
+
172
+ # Retriever tool (optional if you want to expose to agent)
173
+ retriever_tool = create_retriever_tool(
174
+ retriever=vector_store.as_retriever(),
175
+ name="Question Search",
176
+ description="A tool to retrieve similar questions from a vector store.",
177
+ )
178
+
179
+ # Tool list
180
+ tools = [
181
+ multiply, add, subtract, divide, modulus,
182
+ wiki_search, web_search, arvix_search,
183
+ ]
184
+
185
+ # Build graph
186
+ def build_graph(provider: str = "groq"):
187
+
188
+ llm = ChatGroq(model="qwen-qwq-32b", temperature=0,api_key=groq_api_key)
189
+ llm_with_tools = llm.bind_tools(tools)
190
+
191
+ def assistant(state: MessagesState):
192
+ return {"messages": [llm_with_tools.invoke(state["messages"])]}
193
+
194
+ def retriever(state: MessagesState):
195
+ similar = vector_store.similarity_search(state["messages"][0].content)
196
+ if similar:
197
+ example_msg = HumanMessage(content=f"Here is a similar question:\n\n{similar[0].page_content}")
198
+ return {"messages": [sys_msg] + state["messages"] + [example_msg]}
199
+ return {"messages": [sys_msg] + state["messages"]}
200
+
201
+ builder = StateGraph(MessagesState)
202
+ builder.add_node("retriever", retriever)
203
+ builder.add_node("assistant", assistant)
204
+ builder.add_node("tools", ToolNode(tools))
205
+ builder.add_edge(START, "retriever")
206
+ builder.add_edge("retriever", "assistant")
207
+ builder.add_conditional_edges("assistant", tools_condition)
208
+ builder.add_edge("tools", "assistant")
209
+
210
+ return builder.compile()
app.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ # (Keep Constants as is)
11
+ # --- Constants ---
12
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
+
14
+ # --- Basic Agent Definition ---
15
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
+ HF_TOKEN = os.getenv("HF_TOKEN")
17
+
18
+
19
+ class BasicAgent:
20
+ def __init__(self):
21
+ print("SmartAgent initialized.")
22
+ self.graph = build_graph()
23
+
24
+ def __call__(self, question: str) -> str:
25
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
26
+ # Wrap the question in a HumanMessage from langchain_core
27
+ messages = [HumanMessage(content=question)]
28
+ messages = self.graph.invoke({"messages": messages})
29
+ answer = messages['messages'][-1].content
30
+ return answer[14:]
31
+
32
+
33
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
34
+ """
35
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
36
+ and displays the results.
37
+ """
38
+ # --- Determine HF Space Runtime URL and Repo URL ---
39
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
40
+
41
+ if profile:
42
+ username= f"{profile.username}"
43
+ print(f"User logged in: {username}")
44
+ else:
45
+ print("User not logged in.")
46
+ return "Please Login to Hugging Face with the button.", None
47
+
48
+ api_url = DEFAULT_API_URL
49
+ questions_url = f"{api_url}/questions"
50
+ submit_url = f"{api_url}/submit"
51
+
52
+ # 1. Instantiate Agent ( modify this part to create your agent)
53
+ try:
54
+ agent = BasicAgent()
55
+ except Exception as e:
56
+ print(f"Error instantiating agent: {e}")
57
+ return f"Error initializing agent: {e}", None
58
+ # 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)
59
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
60
+ print(agent_code)
61
+
62
+ # 2. Fetch Questions
63
+ print(f"Fetching questions from: {questions_url}")
64
+ try:
65
+ response = requests.get(questions_url, timeout=15)
66
+ response.raise_for_status()
67
+ questions_data = response.json()
68
+ if not questions_data:
69
+ print("Fetched questions list is empty.")
70
+ return "Fetched questions list is empty or invalid format.", None
71
+ print(f"Fetched {len(questions_data)} questions.")
72
+ except requests.exceptions.RequestException as e:
73
+ print(f"Error fetching questions: {e}")
74
+ return f"Error fetching questions: {e}", None
75
+ except requests.exceptions.JSONDecodeError as e:
76
+ print(f"Error decoding JSON response from questions endpoint: {e}")
77
+ print(f"Response text: {response.text[:500]}")
78
+ return f"Error decoding server response for questions: {e}", None
79
+ except Exception as e:
80
+ print(f"An unexpected error occurred fetching questions: {e}")
81
+ return f"An unexpected error occurred fetching questions: {e}", None
82
+
83
+ # 3. Run your Agent
84
+ results_log = []
85
+ answers_payload = []
86
+ print(f"Running agent on {len(questions_data)} questions...")
87
+ for item in questions_data:
88
+ task_id = item.get("task_id")
89
+ question_text = item.get("question")
90
+ if not task_id or question_text is None:
91
+ print(f"Skipping item with missing task_id or question: {item}")
92
+ continue
93
+ try:
94
+ submitted_answer = agent(question_text)
95
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
96
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
97
+ except Exception as e:
98
+ print(f"Error running agent on task {task_id}: {e}")
99
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
100
+
101
+ if not answers_payload:
102
+ print("Agent did not produce any answers to submit.")
103
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
104
+
105
+ # 4. Prepare Submission
106
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
107
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
108
+ print(status_update)
109
+
110
+ # 5. Submit
111
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
112
+ try:
113
+ response = requests.post(submit_url, json=submission_data, timeout=60)
114
+ response.raise_for_status()
115
+ result_data = response.json()
116
+ final_status = (
117
+ f"Submission Successful!\n"
118
+ f"User: {result_data.get('username')}\n"
119
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
120
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
121
+ f"Message: {result_data.get('message', 'No message received.')}"
122
+ )
123
+ print("Submission successful.")
124
+ results_df = pd.DataFrame(results_log)
125
+ return final_status, results_df
126
+ except requests.exceptions.HTTPError as e:
127
+ error_detail = f"Server responded with status {e.response.status_code}."
128
+ try:
129
+ error_json = e.response.json()
130
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
131
+ except requests.exceptions.JSONDecodeError:
132
+ error_detail += f" Response: {e.response.text[:500]}"
133
+ status_message = f"Submission Failed: {error_detail}"
134
+ print(status_message)
135
+ results_df = pd.DataFrame(results_log)
136
+ return status_message, results_df
137
+ except requests.exceptions.Timeout:
138
+ status_message = "Submission Failed: The request timed out."
139
+ print(status_message)
140
+ results_df = pd.DataFrame(results_log)
141
+ return status_message, results_df
142
+ except requests.exceptions.RequestException as e:
143
+ status_message = f"Submission Failed: Network error - {e}"
144
+ print(status_message)
145
+ results_df = pd.DataFrame(results_log)
146
+ return status_message, results_df
147
+ except Exception as e:
148
+ status_message = f"An unexpected error occurred during submission: {e}"
149
+ print(status_message)
150
+ results_df = pd.DataFrame(results_log)
151
+ return status_message, results_df
152
+
153
+
154
+ # --- Build Gradio Interface using Blocks ---
155
+ with gr.Blocks() as demo:
156
+ gr.Markdown("# Basic Agent Evaluation Runner")
157
+ gr.Markdown(
158
+ """
159
+ **Instructions:**
160
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
161
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
162
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
163
+ ---
164
+ **Disclaimers:**
165
+ 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).
166
+ 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.
167
+ """
168
+ )
169
+
170
+ gr.LoginButton()
171
+
172
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
173
+
174
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
175
+ # Removed max_rows=10 from DataFrame constructor
176
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
177
+
178
+ run_button.click(
179
+ fn=run_and_submit_all,
180
+ outputs=[status_output, results_table]
181
+ )
182
+
183
+ if __name__ == "__main__":
184
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
185
+ # Check for SPACE_HOST and SPACE_ID at startup for information
186
+ space_host_startup = os.getenv("SPACE_HOST")
187
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
188
+
189
+ if space_host_startup:
190
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
191
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
192
+ else:
193
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
194
+
195
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
196
+ print(f"✅ SPACE_ID found: {space_id_startup}")
197
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
198
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
199
+ else:
200
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
201
+
202
+ print("-"*(60 + len(" App Starting ")) + "\n")
203
+
204
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
205
+ demo.launch(debug=True, share=False)
metadata.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ requests
3
+ langchain
4
+ langchain-community
5
+ langchain-core
6
+ langchain-google-genai
7
+ langchain-huggingface
8
+ langchain-groq
9
+ langchain-tavily
10
+ langchain-chroma
11
+ langgraph
12
+ huggingface_hub
13
+ arxiv
14
+ pymupdf
15
+ wikipedia
16
+ pgvector
17
+ python-dotenv
18
+ protobuf==3.20.*
19
+ chromadb