Mohammad Haghir commited on
Commit ·
f4c7ecf
1
Parent(s): a0ec674
simple solution
Browse files- app.py +28 -1
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import json
|
|
| 8 |
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
from langchain_core.messages import HumanMessage
|
|
|
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
@@ -25,11 +26,36 @@ llm = ChatGroq(api_key=groq_api_key, model="llama-3.3-70b-versatile")
|
|
| 25 |
class BasicAgent:
|
| 26 |
def __init__(self):
|
| 27 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def __call__(self, question: str) -> str:
|
| 29 |
|
| 30 |
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 31 |
# fixed_answer = "This is a default answer. --- 1"
|
| 32 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
|
|
|
|
|
|
| 33 |
prompt = f"""
|
| 34 |
You are a general AI assistant. I will ask you a question.
|
| 35 |
YOUR FINAL ANSWER should be a number OR
|
|
@@ -39,7 +65,8 @@ class BasicAgent:
|
|
| 39 |
a string, don't use articles, neither abbreviations (e.g. for cities), and write
|
| 40 |
the digits in plain text unless specified otherwise. If you are asked for a comma
|
| 41 |
separated list, apply the above rules depending of whether the element to be put
|
| 42 |
-
in the list is a number or a string. Question: {question}
|
|
|
|
| 43 |
# Your answer must be in the following format:
|
| 44 |
|
| 45 |
# {{"task_id": "task_id_1", "model_answer": "Answer 1 from your model", "reasoning_trace": "The different steps by which your model reached answer 1"}}
|
|
|
|
| 8 |
|
| 9 |
from langchain_groq import ChatGroq
|
| 10 |
from langchain_core.messages import HumanMessage
|
| 11 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
|
|
|
| 26 |
class BasicAgent:
|
| 27 |
def __init__(self):
|
| 28 |
print("BasicAgent initialized.")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def wiki_ret(self, question: str) -> str:
|
| 32 |
+
""" Retrieve docs from wikipedia """
|
| 33 |
+
|
| 34 |
+
# Search query
|
| 35 |
+
# structured_llm = llm.with_structured_output(SearchQuery)
|
| 36 |
+
# search_query = structured_llm.invoke([search_instructions]+state['messages'])
|
| 37 |
+
|
| 38 |
+
# Search
|
| 39 |
+
search_docs = WikipediaLoader(query=question,
|
| 40 |
+
load_max_docs=2).load()
|
| 41 |
+
|
| 42 |
+
# Format
|
| 43 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 44 |
+
[
|
| 45 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 46 |
+
for doc in search_docs
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return formatted_search_docs
|
| 51 |
+
|
| 52 |
def __call__(self, question: str) -> str:
|
| 53 |
|
| 54 |
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 55 |
# fixed_answer = "This is a default answer. --- 1"
|
| 56 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 57 |
+
context = self.wiki_ret(question)
|
| 58 |
+
|
| 59 |
prompt = f"""
|
| 60 |
You are a general AI assistant. I will ask you a question.
|
| 61 |
YOUR FINAL ANSWER should be a number OR
|
|
|
|
| 65 |
a string, don't use articles, neither abbreviations (e.g. for cities), and write
|
| 66 |
the digits in plain text unless specified otherwise. If you are asked for a comma
|
| 67 |
separated list, apply the above rules depending of whether the element to be put
|
| 68 |
+
in the list is a number or a string. Question: {question}
|
| 69 |
+
For answering questions you can use context from wikiperdia: {context}"""
|
| 70 |
# Your answer must be in the following format:
|
| 71 |
|
| 72 |
# {{"task_id": "task_id_1", "model_answer": "Answer 1 from your model", "reasoning_trace": "The different steps by which your model reached answer 1"}}
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
-
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
+
langchain-groq
|
| 4 |
+
langchain-community
|