Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,19 +34,25 @@ print(f"Model {chat.model} downloaded!")
|
|
| 34 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 35 |
|
| 36 |
|
| 37 |
-
def get_file_path(
|
| 38 |
"""Retrieves reference file path."""
|
| 39 |
-
if question
|
| 40 |
return question['file_path']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def get_ref_content(path: str) -> str | object:
|
| 44 |
"""Retrieves content from the reference path provided."""
|
| 45 |
-
|
| 46 |
-
file =
|
|
|
|
|
|
|
|
|
|
| 47 |
return file
|
| 48 |
|
| 49 |
-
|
| 50 |
def search_web(topic: str) -> str:
|
| 51 |
"""Retrieves information about the topic."""
|
| 52 |
results = DuckDuckGoSearchRun().invoke(topic)
|
|
@@ -131,23 +137,15 @@ builder.add_edge("tools", "assistant")
|
|
| 131 |
alfred = builder.compile()
|
| 132 |
system_prompt = SystemMessage(
|
| 133 |
content="You are a general AI assistant. \
|
| 134 |
-
I will ask you a question. Report your thoughts shortly, \
|
| 135 |
-
and finish your answer with the following template: \
|
| 136 |
FINAL ANSWER: [YOUR FINAL ANSWER]. \
|
| 137 |
-
YOUR FINAL ANSWER should be a number OR as few words as possible \
|
| 138 |
-
|
| 139 |
-
If you are asked for a
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
If you are asked for a comma separated list, apply the above rules \
|
| 145 |
-
depending of whether the element to be put in the list is a number or a string. \
|
| 146 |
-
If the question refers to an external content and there is no reference file attached, \
|
| 147 |
-
perform a web search and retrieve relevant information from the internet. \
|
| 148 |
-
Make sure that each final answer is preceded with 'FINAL ANSWER:'. "
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
|
| 152 |
class BasicAgent:
|
| 153 |
def __init__(self):
|
|
@@ -158,8 +156,7 @@ class BasicAgent:
|
|
| 158 |
message = HumanMessage(content=question)
|
| 159 |
print(message)
|
| 160 |
answer = alfred.invoke(input={"messages": [system_prompt, message]},config={"recursion_limit": 3})['messages'][-1].content
|
| 161 |
-
|
| 162 |
-
#answer = "".join(re.findall(r'(FINAL ANSWER:.*)', answer, flags=re.M))
|
| 163 |
answer = answer.replace('FINAL ANSWER: ', '')
|
| 164 |
answer = answer.replace('[', '')
|
| 165 |
nswer = answer.replace('*', '')
|
|
|
|
| 34 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 35 |
|
| 36 |
|
| 37 |
+
def get_file_path(question: str) -> str:
|
| 38 |
"""Retrieves reference file path."""
|
| 39 |
+
if isinstance(question, dict):
|
| 40 |
return question['file_path']
|
| 41 |
+
elif isinstance(question, str):
|
| 42 |
+
for q in question_dataset:
|
| 43 |
+
if q['Question'] == question:
|
| 44 |
+
return q['file_path']
|
| 45 |
|
| 46 |
|
| 47 |
def get_ref_content(path: str) -> str | object:
|
| 48 |
"""Retrieves content from the reference path provided."""
|
| 49 |
+
if path.startswith('http:'):
|
| 50 |
+
file = requests.get(path).content
|
| 51 |
+
else:
|
| 52 |
+
with open(path, "rb") as f:
|
| 53 |
+
file = f.read()
|
| 54 |
return file
|
| 55 |
|
|
|
|
| 56 |
def search_web(topic: str) -> str:
|
| 57 |
"""Retrieves information about the topic."""
|
| 58 |
results = DuckDuckGoSearchRun().invoke(topic)
|
|
|
|
| 137 |
alfred = builder.compile()
|
| 138 |
system_prompt = SystemMessage(
|
| 139 |
content="You are a general AI assistant. \
|
| 140 |
+
I will ask you a question. Report your thoughts shortly, and finish your answer with the following template: \
|
|
|
|
| 141 |
FINAL ANSWER: [YOUR FINAL ANSWER]. \
|
| 142 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. \
|
| 143 |
+
If you are asked for a number, use only digits in your final answer. Don't use comma nor brackets to write your number neither use units such as $ or percent sign unless specified otherwise. \
|
| 144 |
+
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. \
|
| 145 |
+
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. \
|
| 146 |
+
If the question refers to an external content and there is no reference file attached, perform a web search and retrieve relevant information from the internet. \
|
| 147 |
+
Make sure that each final answer is preceded with 'FINAL ANSWER:' and is short: it should contain a number, a string or a comma-separated list, nothing else. "
|
| 148 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
class BasicAgent:
|
| 151 |
def __init__(self):
|
|
|
|
| 156 |
message = HumanMessage(content=question)
|
| 157 |
print(message)
|
| 158 |
answer = alfred.invoke(input={"messages": [system_prompt, message]},config={"recursion_limit": 3})['messages'][-1].content
|
| 159 |
+
answer = "".join(re.findall(r'(FINAL ANSWER:.*)', answer, flags=re.M))
|
|
|
|
| 160 |
answer = answer.replace('FINAL ANSWER: ', '')
|
| 161 |
answer = answer.replace('[', '')
|
| 162 |
nswer = answer.replace('*', '')
|