Spaces:
Sleeping
Sleeping
Minor code updates
Browse files
app.py
CHANGED
|
@@ -10,28 +10,7 @@ import gradio as gr
|
|
| 10 |
hf_api_key = os.environ.get('HF_API_KEY')
|
| 11 |
client = OpenAI() # Assumes OPENAI_API_KEY is set in environment
|
| 12 |
|
| 13 |
-
|
| 14 |
-
class Agent:
|
| 15 |
-
def __init__(self, system=""):
|
| 16 |
-
self.system = system
|
| 17 |
-
self.messages = []
|
| 18 |
-
if self.system:
|
| 19 |
-
self.messages.append({"role": "system", "content": system})
|
| 20 |
-
|
| 21 |
-
def __call__(self, message):
|
| 22 |
-
self.messages.append({"role": "user", "content": message})
|
| 23 |
-
result = self.execute()
|
| 24 |
-
self.messages.append({"role": "assistant", "content": result})
|
| 25 |
-
return result
|
| 26 |
-
|
| 27 |
-
def execute(self):
|
| 28 |
-
completion = client.chat.completions.create(
|
| 29 |
-
model="gpt-4o",
|
| 30 |
-
temperature=0,
|
| 31 |
-
messages=self.messages)
|
| 32 |
-
return completion.choices[0].message.content
|
| 33 |
-
|
| 34 |
-
|
| 35 |
prompt = """
|
| 36 |
You run in a loop of Thought, Action, PAUSE, Observation.
|
| 37 |
At the end of the loop you output an Answer
|
|
@@ -65,6 +44,27 @@ You then output:
|
|
| 65 |
Answer: A bulldog weights 51 lbs
|
| 66 |
""".strip()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def calculate(what):
|
| 69 |
return eval(what)
|
| 70 |
|
|
@@ -78,11 +78,13 @@ def average_dog_weight(name):
|
|
| 78 |
else:
|
| 79 |
return("An average dog weights 50 lbs")
|
| 80 |
|
|
|
|
| 81 |
known_actions = {
|
| 82 |
"calculate": calculate,
|
| 83 |
"average_dog_weight": average_dog_weight
|
| 84 |
}
|
| 85 |
|
|
|
|
| 86 |
abot = Agent(prompt)
|
| 87 |
|
| 88 |
result = abot("How much does a toy poodle weigh?")
|
|
@@ -98,6 +100,7 @@ abot(next_prompt)
|
|
| 98 |
|
| 99 |
print(abot.messages)
|
| 100 |
|
|
|
|
| 101 |
abot = Agent(prompt)
|
| 102 |
|
| 103 |
question = """I have 2 dogs, a border collie and a scottish terrier. \
|
|
@@ -124,6 +127,7 @@ action_re = re.compile('^Action: (\w+): (.*)$') # python regular expression to
|
|
| 124 |
@spaces.GPU(duration=120) # Designed to be effect-free in non-ZeroGPU environments, ensuring compatibility across different setups.
|
| 125 |
def query(question, max_turns=5):
|
| 126 |
i = 0
|
|
|
|
| 127 |
bot = Agent(prompt)
|
| 128 |
next_prompt = question
|
| 129 |
while i < max_turns:
|
|
@@ -144,8 +148,6 @@ def query(question, max_turns=5):
|
|
| 144 |
return result
|
| 145 |
return "Max turns reached"
|
| 146 |
|
| 147 |
-
#question = """I have 2 dogs, a border collie and a scottish terrier. What is their combined weight"""
|
| 148 |
-
#query(question)
|
| 149 |
|
| 150 |
# Gradio interface
|
| 151 |
def process_question(question):
|
|
@@ -153,7 +155,7 @@ def process_question(question):
|
|
| 153 |
|
| 154 |
iface = gr.Interface(
|
| 155 |
fn=process_question,
|
| 156 |
-
inputs=gr.Textbox(label="Enter your question"),
|
| 157 |
outputs=gr.Textbox(label="Answer"),
|
| 158 |
title="Dog Weight Calculator",
|
| 159 |
description="Ask about dog weights or perform calculations."
|
|
|
|
| 10 |
hf_api_key = os.environ.get('HF_API_KEY')
|
| 11 |
client = OpenAI() # Assumes OPENAI_API_KEY is set in environment
|
| 12 |
|
| 13 |
+
# Default system prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
prompt = """
|
| 15 |
You run in a loop of Thought, Action, PAUSE, Observation.
|
| 16 |
At the end of the loop you output an Answer
|
|
|
|
| 44 |
Answer: A bulldog weights 51 lbs
|
| 45 |
""".strip()
|
| 46 |
|
| 47 |
+
class Agent:
|
| 48 |
+
def __init__(self, system=""):
|
| 49 |
+
self.system = system
|
| 50 |
+
self.messages = []
|
| 51 |
+
if self.system:
|
| 52 |
+
self.messages.append({"role": "system", "content": system})
|
| 53 |
+
|
| 54 |
+
def __call__(self, message):
|
| 55 |
+
self.messages.append({"role": "user", "content": message})
|
| 56 |
+
result = self.execute()
|
| 57 |
+
self.messages.append({"role": "assistant", "content": result})
|
| 58 |
+
return result
|
| 59 |
+
|
| 60 |
+
def execute(self):
|
| 61 |
+
completion = client.chat.completions.create(
|
| 62 |
+
model="gpt-4o",
|
| 63 |
+
temperature=0,
|
| 64 |
+
messages=self.messages)
|
| 65 |
+
return completion.choices[0].message.content
|
| 66 |
+
|
| 67 |
+
# Tools
|
| 68 |
def calculate(what):
|
| 69 |
return eval(what)
|
| 70 |
|
|
|
|
| 78 |
else:
|
| 79 |
return("An average dog weights 50 lbs")
|
| 80 |
|
| 81 |
+
# Available actions
|
| 82 |
known_actions = {
|
| 83 |
"calculate": calculate,
|
| 84 |
"average_dog_weight": average_dog_weight
|
| 85 |
}
|
| 86 |
|
| 87 |
+
# First manual example, use of LLM agent answering a query
|
| 88 |
abot = Agent(prompt)
|
| 89 |
|
| 90 |
result = abot("How much does a toy poodle weigh?")
|
|
|
|
| 100 |
|
| 101 |
print(abot.messages)
|
| 102 |
|
| 103 |
+
# Second manual example, use of LLM agent answering a query
|
| 104 |
abot = Agent(prompt)
|
| 105 |
|
| 106 |
question = """I have 2 dogs, a border collie and a scottish terrier. \
|
|
|
|
| 127 |
@spaces.GPU(duration=120) # Designed to be effect-free in non-ZeroGPU environments, ensuring compatibility across different setups.
|
| 128 |
def query(question, max_turns=5):
|
| 129 |
i = 0
|
| 130 |
+
# Create agent based on default system prompt
|
| 131 |
bot = Agent(prompt)
|
| 132 |
next_prompt = question
|
| 133 |
while i < max_turns:
|
|
|
|
| 148 |
return result
|
| 149 |
return "Max turns reached"
|
| 150 |
|
|
|
|
|
|
|
| 151 |
|
| 152 |
# Gradio interface
|
| 153 |
def process_question(question):
|
|
|
|
| 155 |
|
| 156 |
iface = gr.Interface(
|
| 157 |
fn=process_question,
|
| 158 |
+
inputs=gr.Textbox(label="Enter your question"), # e.g. I have 2 dogs, a border collie and a scottish terrier. What is their combined weight
|
| 159 |
outputs=gr.Textbox(label="Answer"),
|
| 160 |
title="Dog Weight Calculator",
|
| 161 |
description="Ask about dog weights or perform calculations."
|