Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import re
|
| 4 |
+
from duckduckgo_search import DDGS
|
| 5 |
+
|
| 6 |
+
SYSPROMPT = """You are a Time-Travel Consultant who helps travelers blend into different historical periods.
|
| 7 |
+
You must think step by step and use available tools when needed.
|
| 8 |
+
|
| 9 |
+
## Thought Process:
|
| 10 |
+
1. Consider the user’s travel destination and time period. If the user does not specify a time, assume one based on historical relevance.
|
| 11 |
+
2. Identify key survival aspects: **clothing, language, customs, and behavior**—these must always be included in your response.
|
| 12 |
+
3. If additional knowledge is required, use the appropriate tool.
|
| 13 |
+
4. Incorporate the tool’s response into your reasoning.
|
| 14 |
+
5. Conclude with a complete recommendation. Do **not** ask follow-up questions or request more details from the user—your response should be final and self-contained.
|
| 15 |
+
|
| 16 |
+
## Tool Usage Format:
|
| 17 |
+
If you need to use a tool, respond with:
|
| 18 |
+
[ACTION: tool_name("query")]
|
| 19 |
+
|
| 20 |
+
After receiving a tool response, continue reasoning with the new information.
|
| 21 |
+
|
| 22 |
+
## Important Guidelines:
|
| 23 |
+
- If the user input **does not make sense** (e.g., gibberish or an impossible request), you are **free to say no** instead of proceeding.
|
| 24 |
+
- If no tool can provide useful information, explain why and suggest an alternative.
|
| 25 |
+
- Do **not** invent tools that are not listed.
|
| 26 |
+
- Do **not** ask the user questions or seek clarification—**always give a complete response based on the available information.**
|
| 27 |
+
|
| 28 |
+
## Available Tools:
|
| 29 |
+
- **search(query)**: Finds historical facts (e.g., "Ancient Rome clothing", "Currency", etc.).
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
FIN_PROMPT = """
|
| 33 |
+
You are a charismatic and witty Time-Travel Consultant.
|
| 34 |
+
|
| 35 |
+
Take the following assistant response, which may contain tool references, and rewrite it in a fun and engaging way.
|
| 36 |
+
- Remove any mentions of tools, actions, or system processes.
|
| 37 |
+
- Rewrite the information in a way that makes it sound **natural, humorous, and engaging.**
|
| 38 |
+
- If the answer is obvious or ridiculous, feel free to be sarcastic or dramatic.
|
| 39 |
+
- Ensure it is still **historically accurate** but entertaining.
|
| 40 |
+
|
| 41 |
+
## Example:
|
| 42 |
+
**Input:**
|
| 43 |
+
_"To blend into Ancient Rome, you should wear a tunic, as it was the common attire. Wealthier individuals would wear togas."_
|
| 44 |
+
|
| 45 |
+
**Output:**
|
| 46 |
+
_"Ah, Ancient Rome! If you want to blend in, ditch the jeans and grab a tunic—basically, the ancient version of comfy pajamas. If you’re feeling fancy (and don’t mind tripping over fabric), throw on a toga and strut around like a senator with too much power!"_
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
class TimeAdvisor:
|
| 50 |
+
def __init__(self):
|
| 51 |
+
self.client = Groq(
|
| 52 |
+
api_key=os.environ.get("GROQ_API_KEY"),
|
| 53 |
+
)
|
| 54 |
+
self.sys_prompt = SYSPROMPT
|
| 55 |
+
self.history = [{
|
| 56 |
+
"role": "system",
|
| 57 |
+
"content": self.sys_prompt,
|
| 58 |
+
}]
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def llm_call(self, query):
|
| 63 |
+
self.history.append({
|
| 64 |
+
"role": "user",
|
| 65 |
+
"content": query,
|
| 66 |
+
})
|
| 67 |
+
chat_completion = self.client.chat.completions.create(
|
| 68 |
+
messages=self.history,
|
| 69 |
+
model="llama-3.3-70b-versatile",
|
| 70 |
+
)
|
| 71 |
+
self.history.append({
|
| 72 |
+
"role": "assistant",
|
| 73 |
+
"content": chat_completion.choices[0].message.content,
|
| 74 |
+
})
|
| 75 |
+
self.latest = chat_completion.choices[0].message.content
|
| 76 |
+
|
| 77 |
+
def extract_actions(self, llm_response:str):
|
| 78 |
+
"""Extracts tool calls and queries from LLM response"""
|
| 79 |
+
pattern = r"\[ACTION:\s*(\w+)\(\"(.*?)\"\)\]"
|
| 80 |
+
matches = re.findall(pattern, llm_response)
|
| 81 |
+
|
| 82 |
+
# Convert list of tuples to a structured dictionary format
|
| 83 |
+
actions = [{"tool": tool, "query": query} for tool, query in matches]
|
| 84 |
+
return actions
|
| 85 |
+
|
| 86 |
+
def web_search(self, query):
|
| 87 |
+
web_str = f"for search results of query: {query}, Results:"
|
| 88 |
+
with DDGS() as ddgs:
|
| 89 |
+
results = list(ddgs.text(query, max_results=1))
|
| 90 |
+
return web_str + results[0]["body"] if results else "No relevant data found."
|
| 91 |
+
|
| 92 |
+
def get_tool_results(self,actions):
|
| 93 |
+
tool_results = ""
|
| 94 |
+
for action in actions:
|
| 95 |
+
if action['tool']=="search":
|
| 96 |
+
#print(action["query"])
|
| 97 |
+
tool_results+=self.web_search(action["query"])
|
| 98 |
+
|
| 99 |
+
return tool_results
|
| 100 |
+
|
| 101 |
+
def agent_loop(self, query):
|
| 102 |
+
self.llm_call(query)
|
| 103 |
+
#print(self.latest)
|
| 104 |
+
actions = self.extract_actions(self.latest)
|
| 105 |
+
iters = 0
|
| 106 |
+
while len(actions)>0 and iters<5:
|
| 107 |
+
tool_results = self.get_tool_results(actions)
|
| 108 |
+
self.llm_call(tool_results)
|
| 109 |
+
#print(self.latest)
|
| 110 |
+
actions = self.extract_actions(self.latest)
|
| 111 |
+
iters+=1
|
| 112 |
+
|
| 113 |
+
self.history = [{
|
| 114 |
+
"role": "system",
|
| 115 |
+
"content": FIN_PROMPT,
|
| 116 |
+
}]
|
| 117 |
+
self.llm_call(self.latest)
|
| 118 |
+
|
| 119 |
+
return self.latest
|
| 120 |
+
|
| 121 |
+
if __name__=="__main__":
|
| 122 |
+
advisor = TimeAdvisor()
|
| 123 |
+
output = advisor.agent_loop("Ancient Mesopotamia")
|
| 124 |
+
print(output)
|