Andrei Sferdian commited on
Commit ·
be70cf0
1
Parent(s): d1b1025
Improve local run
Browse files- .env.template +1 -1
- .gitignore +1 -0
- agent.py +1 -114
- app.py +40 -22
- langfuse.py → langfuse_handler.py +0 -0
- questions.py +47 -0
- tasks.py +0 -52
.env.template
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
ENV=local # local, space
|
| 2 |
HF_TOKEN=
|
| 3 |
TOGETHER_API_KEY=
|
| 4 |
-
|
| 5 |
USE_LANGFUSE= # any value to enable (i.e. 1, true, yes, etc.)
|
| 6 |
LANGFUSE_PUBLIC_KEY=
|
| 7 |
LANGFUSE_SECRET_KEY=
|
|
|
|
| 1 |
ENV=local # local, space
|
| 2 |
HF_TOKEN=
|
| 3 |
TOGETHER_API_KEY=
|
| 4 |
+
API_URL=
|
| 5 |
USE_LANGFUSE= # any value to enable (i.e. 1, true, yes, etc.)
|
| 6 |
LANGFUSE_PUBLIC_KEY=
|
| 7 |
LANGFUSE_SECRET_KEY=
|
.gitignore
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
.venv
|
| 2 |
.env
|
| 3 |
__pycache__
|
|
|
|
| 4 |
|
|
|
|
| 1 |
.venv
|
| 2 |
.env
|
| 3 |
__pycache__
|
| 4 |
+
.data
|
| 5 |
|
agent.py
CHANGED
|
@@ -1,16 +1,7 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
from functools import reduce
|
| 3 |
-
|
| 4 |
-
import filetype
|
| 5 |
-
import requests
|
| 6 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
-
from langchain_core.messages import HumanMessage
|
| 8 |
from langchain_together import ChatTogether
|
| 9 |
-
from langgraph.graph import StateGraph
|
| 10 |
from langgraph.prebuilt import create_react_agent
|
| 11 |
-
from langgraph.prebuilt.chat_agent_executor import AgentState
|
| 12 |
|
| 13 |
-
import questions
|
| 14 |
from tools import GetYouTubeTranscript, search_vector_store
|
| 15 |
|
| 16 |
tools = [
|
|
@@ -21,7 +12,7 @@ tools = [
|
|
| 21 |
|
| 22 |
model = ChatTogether(
|
| 23 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 24 |
-
temperature=0,
|
| 25 |
max_tokens=2000,
|
| 26 |
max_retries=3,
|
| 27 |
).bind_tools(tools)
|
|
@@ -31,107 +22,3 @@ agent = create_react_agent(
|
|
| 31 |
tools=tools,
|
| 32 |
# debug=True,
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
builder = StateGraph(state_schema=AgentState)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
class Urls:
|
| 39 |
-
TASKS_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 40 |
-
|
| 41 |
-
@classmethod
|
| 42 |
-
def questions(cls) -> str:
|
| 43 |
-
return f"{cls.TASKS_URL}/questions"
|
| 44 |
-
|
| 45 |
-
@classmethod
|
| 46 |
-
def files(cls, task_id: str) -> str:
|
| 47 |
-
return f"{cls.TASKS_URL}/files/{task_id}"
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if __name__ == "__main__":
|
| 51 |
-
# while True:
|
| 52 |
-
# Get user input
|
| 53 |
-
# user_input = input("Enter your message (or 'exit' to quit): ")
|
| 54 |
-
# if user_input.lower() in ["exit", "quit", "q"]:
|
| 55 |
-
# break
|
| 56 |
-
|
| 57 |
-
questions = questions.get_questions()
|
| 58 |
-
print(f"Fetched {len(questions)} questions.")
|
| 59 |
-
# print(f"Questions: {questions}")
|
| 60 |
-
question = next((q for q in questions if q.get("file_name")), None)
|
| 61 |
-
task_id = question["task_id"]
|
| 62 |
-
if task_id is None:
|
| 63 |
-
print("No file_name found in questions.")
|
| 64 |
-
exit()
|
| 65 |
-
|
| 66 |
-
print(f"Task ID: {task_id}")
|
| 67 |
-
# Attempt to fetch the file
|
| 68 |
-
try:
|
| 69 |
-
res = requests.get(Urls.files(task_id), timeout=15)
|
| 70 |
-
res.raise_for_status()
|
| 71 |
-
file_type = filetype.guess(res.content)
|
| 72 |
-
print(f"File name: {question['file_name']}")
|
| 73 |
-
print(f"File data: {res.content[:100]}...")
|
| 74 |
-
print(f"File type: {file_type}")
|
| 75 |
-
# loader = UnstructuredFileIOLoader(io.BytesIO(res.content))
|
| 76 |
-
# for doc in loader.lazy_load():
|
| 77 |
-
# print(f"Document: {doc}")
|
| 78 |
-
|
| 79 |
-
except requests.HTTPError as e:
|
| 80 |
-
print(f"Status: {e.response.status_code}")
|
| 81 |
-
print(f"Error fetching file: {e}")
|
| 82 |
-
|
| 83 |
-
exit()
|
| 84 |
-
|
| 85 |
-
# Form a single message out of all questions
|
| 86 |
-
questions_batch = reduce(lambda a, q: a + q + "\n", questions, "")
|
| 87 |
-
print(f"Questions batch: {questions_batch}")
|
| 88 |
-
content = (
|
| 89 |
-
"Evaluate the following questions without answering them.\n"
|
| 90 |
-
+ "Instead, provide a list of tools that an AI model might use to answer them.\n"
|
| 91 |
-
+ "Also, flag which tools are prebuilt by the langchain community.\n"
|
| 92 |
-
+ "Return the response in JSON format."
|
| 93 |
-
+ "The response should use the following format:\n"
|
| 94 |
-
+ "{\n"
|
| 95 |
-
+ ' "tools": [\n'
|
| 96 |
-
+ ' {"name": "tool_name", "prebuilt": true},\n'
|
| 97 |
-
+ ' {"name": "tool_name", "prebuilt": false}\n'
|
| 98 |
-
+ " ],\n"
|
| 99 |
-
+ ' "questions": [\n'
|
| 100 |
-
+ ' {"question": "question_text", "tool": "tool_name"},\n'
|
| 101 |
-
+ ' {"question": "question_text", "tool": "tool_name"}\n'
|
| 102 |
-
+ " ]\n"
|
| 103 |
-
+ "}\n"
|
| 104 |
-
+ "The questions are:\n"
|
| 105 |
-
+ questions_batch
|
| 106 |
-
)
|
| 107 |
-
print(f"Content: {content}")
|
| 108 |
-
response = agent.invoke(input={"messages": [HumanMessage(content=content)]})
|
| 109 |
-
print(f"Response: {response['messages'][-1].content}")
|
| 110 |
-
exit()
|
| 111 |
-
|
| 112 |
-
async def answer_question(question: str):
|
| 113 |
-
print(f"Question: {question}")
|
| 114 |
-
try:
|
| 115 |
-
response = await agent.ainvoke(
|
| 116 |
-
{
|
| 117 |
-
"messages": [
|
| 118 |
-
HumanMessage(content=question),
|
| 119 |
-
],
|
| 120 |
-
}
|
| 121 |
-
)
|
| 122 |
-
if (
|
| 123 |
-
not response
|
| 124 |
-
or response.get("messages") is None
|
| 125 |
-
or len(response["messages"]) == 0
|
| 126 |
-
):
|
| 127 |
-
print(f"No messages in response for question: {question}")
|
| 128 |
-
|
| 129 |
-
print(f"Response: {response['messages'][-1].content}")
|
| 130 |
-
except Exception as e:
|
| 131 |
-
print(f"Error invoking agent for question '{question}': {e}")
|
| 132 |
-
|
| 133 |
-
async def main():
|
| 134 |
-
tasks = [answer_question(question) for question in questions]
|
| 135 |
-
await asyncio.gather(*tasks)
|
| 136 |
-
|
| 137 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_community.tools import DuckDuckGoSearchRun
|
|
|
|
| 2 |
from langchain_together import ChatTogether
|
|
|
|
| 3 |
from langgraph.prebuilt import create_react_agent
|
|
|
|
| 4 |
|
|
|
|
| 5 |
from tools import GetYouTubeTranscript, search_vector_store
|
| 6 |
|
| 7 |
tools = [
|
|
|
|
| 12 |
|
| 13 |
model = ChatTogether(
|
| 14 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 15 |
+
temperature=0.3,
|
| 16 |
max_tokens=2000,
|
| 17 |
max_retries=3,
|
| 18 |
).bind_tools(tools)
|
|
|
|
| 22 |
tools=tools,
|
| 23 |
# debug=True,
|
| 24 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
|
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
|
@@ -7,8 +8,8 @@ import requests
|
|
| 7 |
from langchain_core.messages import HumanMessage
|
| 8 |
|
| 9 |
from agent import agent
|
| 10 |
-
from
|
| 11 |
-
from
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
|
@@ -19,19 +20,6 @@ langfuse_handler = (
|
|
| 19 |
)
|
| 20 |
|
| 21 |
|
| 22 |
-
def run():
|
| 23 |
-
"""
|
| 24 |
-
This function is a placeholder for the agent's logic.
|
| 25 |
-
It should be replaced with the actual implementation of the agent.
|
| 26 |
-
"""
|
| 27 |
-
|
| 28 |
-
tasks = get_tasks()
|
| 29 |
-
if not tasks:
|
| 30 |
-
return "No tasks available."
|
| 31 |
-
|
| 32 |
-
return "Agent run completed."
|
| 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,
|
|
@@ -45,7 +33,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 45 |
print("User not logged in.")
|
| 46 |
return "Please Login to Hugging Face with the button.", None
|
| 47 |
|
| 48 |
-
api_url = os.getenv("
|
| 49 |
questions_url = f"{api_url}/questions"
|
| 50 |
submit_url = f"{api_url}/submit"
|
| 51 |
|
|
@@ -265,12 +253,42 @@ def run_in_space():
|
|
| 265 |
|
| 266 |
def run_locally():
|
| 267 |
print("Running locally...")
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
|
| 276 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
|
|
|
| 8 |
from langchain_core.messages import HumanMessage
|
| 9 |
|
| 10 |
from agent import agent
|
| 11 |
+
from langfuse_handler import create_langfuse_handler
|
| 12 |
+
from questions import Answer, Question, get_questions
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 24 |
"""
|
| 25 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 33 |
print("User not logged in.")
|
| 34 |
return "Please Login to Hugging Face with the button.", None
|
| 35 |
|
| 36 |
+
api_url = os.getenv("API_URL") or DEFAULT_API_URL
|
| 37 |
questions_url = f"{api_url}/questions"
|
| 38 |
submit_url = f"{api_url}/submit"
|
| 39 |
|
|
|
|
| 253 |
|
| 254 |
def run_locally():
|
| 255 |
print("Running locally...")
|
| 256 |
+
|
| 257 |
+
questions = get_questions(1)
|
| 258 |
+
if not questions:
|
| 259 |
+
print("No questions available to run.")
|
| 260 |
+
return
|
| 261 |
+
|
| 262 |
+
max_concurrency = int(os.getenv("MAX_CONCURRENCY", 1))
|
| 263 |
+
with ThreadPoolExecutor(max_workers=max_concurrency) as executor:
|
| 264 |
+
answers = list(executor.map(process_question, questions))
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def process_question(question: Question) -> Answer:
|
| 268 |
+
task_id = question.task_id
|
| 269 |
+
question_text = question.question
|
| 270 |
+
answer = Answer(task_id=task_id, answer="")
|
| 271 |
+
print(f"Processing question: {question}")
|
| 272 |
+
try:
|
| 273 |
+
response = agent.invoke(
|
| 274 |
+
{
|
| 275 |
+
"messages": [
|
| 276 |
+
HumanMessage(content=question_text),
|
| 277 |
+
],
|
| 278 |
+
},
|
| 279 |
+
)
|
| 280 |
+
answer_text = (
|
| 281 |
+
response["messages"][-1].content
|
| 282 |
+
if response and response.get("messages") and len(response["messages"]) > 0
|
| 283 |
+
else ""
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
except Exception as e:
|
| 287 |
+
answer_text = f"AGENT ERROR: {e}"
|
| 288 |
+
|
| 289 |
+
answer = Answer(task_id=task_id, answer=answer_text)
|
| 290 |
+
print(f"Answer: {answer.answer}")
|
| 291 |
+
return answer
|
| 292 |
|
| 293 |
|
| 294 |
if __name__ == "__main__":
|
langfuse.py → langfuse_handler.py
RENAMED
|
File without changes
|
questions.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
import requests
|
| 5 |
+
from pydantic import BaseModel, Field
|
| 6 |
+
|
| 7 |
+
API_URL = os.getenv("API_URL") or "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class Question(BaseModel):
|
| 11 |
+
task_id: str = Field(..., min_length=1)
|
| 12 |
+
question: str = Field(..., min_length=1)
|
| 13 |
+
file_path: str = ""
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class Answer(BaseModel):
|
| 17 |
+
task_id: str = Field(..., min_length=1)
|
| 18 |
+
answer: str = ""
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_questions(limit: int = None) -> list[Question]:
|
| 22 |
+
questions_url = API_URL + "/questions"
|
| 23 |
+
print(f"Fetching questions from: {questions_url}")
|
| 24 |
+
try:
|
| 25 |
+
response = requests.get(questions_url, timeout=15)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
questions_list = response.json()
|
| 28 |
+
if not isinstance(questions_list, list):
|
| 29 |
+
print("Invalid response format. Expected a list.")
|
| 30 |
+
return []
|
| 31 |
+
questions = [Question(**item) for item in questions_list]
|
| 32 |
+
if not questions:
|
| 33 |
+
print("Received empty task list.")
|
| 34 |
+
return []
|
| 35 |
+
|
| 36 |
+
return random.sample(
|
| 37 |
+
questions,
|
| 38 |
+
min(
|
| 39 |
+
limit,
|
| 40 |
+
len(questions),
|
| 41 |
+
)
|
| 42 |
+
if limit is not None
|
| 43 |
+
else len(questions),
|
| 44 |
+
)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Error fetching questions: {e}")
|
| 47 |
+
return []
|
tasks.py
DELETED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import random
|
| 3 |
-
|
| 4 |
-
import requests
|
| 5 |
-
|
| 6 |
-
TASKS_URL = os.getenv("TASKS_URL") or "https://agents-course-unit4-scoring.hf.space"
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class Task:
|
| 10 |
-
def __init__(self, task_id: str, question: str, file_name: str = ""):
|
| 11 |
-
if not task_id:
|
| 12 |
-
raise ValueError("task_id cannot be empty")
|
| 13 |
-
|
| 14 |
-
if not question:
|
| 15 |
-
raise ValueError("question cannot be empty")
|
| 16 |
-
|
| 17 |
-
self.task_id = task_id
|
| 18 |
-
self.question = question
|
| 19 |
-
self.file_name = file_name
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def get_tasks(limit: int = None) -> list[Task]:
|
| 23 |
-
questions_url = TASKS_URL + "/questions"
|
| 24 |
-
print(f"Fetching tasks from: {questions_url}")
|
| 25 |
-
try:
|
| 26 |
-
response = requests.get(questions_url, timeout=15)
|
| 27 |
-
response.raise_for_status()
|
| 28 |
-
tasks = [Task(**item) for item in response.json()]
|
| 29 |
-
if not tasks:
|
| 30 |
-
print("Fetched tasks list is empty or invalid format.")
|
| 31 |
-
return []
|
| 32 |
-
|
| 33 |
-
print(f"Fetched {len(tasks)} tasks. Returning {limit} tasks.")
|
| 34 |
-
return random.sample(
|
| 35 |
-
tasks,
|
| 36 |
-
min(
|
| 37 |
-
limit,
|
| 38 |
-
len(tasks),
|
| 39 |
-
)
|
| 40 |
-
if limit is not None
|
| 41 |
-
else len(tasks),
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
except requests.exceptions.RequestException as e:
|
| 45 |
-
print(f"Error fetching tasks: {e}")
|
| 46 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 47 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 48 |
-
print(f"Response text: {response.text[:500]}")
|
| 49 |
-
except Exception as e:
|
| 50 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
| 51 |
-
|
| 52 |
-
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|