Spaces:
Sleeping
Sleeping
Kate Forsberg
commited on
Commit
·
7d58ec4
1
Parent(s):
2389189
anon
Browse files
app.py
CHANGED
@@ -4,7 +4,12 @@ from typing import Any
|
|
4 |
from dotenv import load_dotenv
|
5 |
from griptape.structures import Agent
|
6 |
from griptape.tasks import PromptTask
|
7 |
-
from griptape.drivers import
|
|
|
|
|
|
|
|
|
|
|
8 |
from griptape.memory.structure import ConversationMemory
|
9 |
from griptape.tools import StructureRunClient, FileManager
|
10 |
from griptape.rules import Rule, Ruleset
|
@@ -14,21 +19,22 @@ import os
|
|
14 |
import re
|
15 |
|
16 |
|
17 |
-
|
18 |
-
#Load environment variables
|
19 |
load_dotenv()
|
20 |
|
21 |
|
22 |
-
#Create an agent that will create a prompt that can be used as input for the query agent from the Griptape Cloud.
|
|
|
23 |
|
24 |
-
#Function that logs user history - adds to history parameter of Gradio
|
25 |
-
#TODO: Figure out the exact use of this function
|
26 |
def user(user_message, history):
|
27 |
history.append([user_message, None])
|
28 |
return ("", history)
|
29 |
|
30 |
-
|
31 |
-
#
|
|
|
32 |
def bot(history):
|
33 |
response = send_message(history[-1][0])
|
34 |
history[-1][1] = ""
|
@@ -40,7 +46,7 @@ def bot(history):
|
|
40 |
yield history
|
41 |
|
42 |
|
43 |
-
def create_prompt_task(session_id:str, message:str) -> PromptTask:
|
44 |
return PromptTask(
|
45 |
f"""
|
46 |
Re-structure the values from the user's questions: '{message}' and the input value from the conversation memory '{session_id}.json' to fit the following format. Leave out attributes that aren't important to the user:
|
@@ -54,17 +60,18 @@ def create_prompt_task(session_id:str, message:str) -> PromptTask:
|
|
54 |
past projects: <x>
|
55 |
show reel details: <x>
|
56 |
""",
|
57 |
-
|
|
|
58 |
|
59 |
-
def build_talk_agent(session_id:str,message:str) -> Agent:
|
60 |
ruleset = Ruleset(
|
61 |
name="Local Gradio Agent",
|
62 |
rules=[
|
63 |
Rule(
|
64 |
-
value
|
65 |
),
|
66 |
Rule(
|
67 |
-
value
|
68 |
),
|
69 |
Rule(
|
70 |
value="If the user has no preference for a specific attribute, then you can remove it from the query."
|
@@ -72,43 +79,42 @@ def build_talk_agent(session_id:str,message:str) -> Agent:
|
|
72 |
Rule(
|
73 |
value="Only return the current query structure and any questions to fill in missing information."
|
74 |
),
|
75 |
-
|
|
|
|
|
|
|
76 |
)
|
77 |
file_manager_tool = FileManager(
|
78 |
name="FileManager",
|
79 |
file_manager_driver=LocalFileManagerDriver(),
|
80 |
-
off_prompt=False
|
81 |
)
|
82 |
|
83 |
return Agent(
|
84 |
-
config=
|
85 |
conversation_memory=ConversationMemory(
|
86 |
-
|
87 |
-
|
88 |
-
)),
|
89 |
tools=[file_manager_tool],
|
90 |
-
tasks=[create_prompt_task(session_id,message)],
|
91 |
rulesets=[ruleset],
|
92 |
)
|
93 |
|
94 |
|
|
|
|
|
|
|
95 |
|
96 |
-
# Creates an agent for each run
|
97 |
-
# The agent uses local memory, which it differentiates between by session_hash.
|
98 |
-
def build_agent(session_id:str,message:str) -> Agent:
|
99 |
-
|
100 |
ruleset = Ruleset(
|
101 |
name="Local Gradio Agent",
|
102 |
rules=[
|
103 |
Rule(
|
104 |
-
value
|
105 |
),
|
106 |
Rule(
|
107 |
value="Only return the result of the query, do not provide additional commentary."
|
108 |
),
|
109 |
-
Rule(
|
110 |
-
value="Only perform one task at a time."
|
111 |
-
),
|
112 |
Rule(
|
113 |
value="Do not perform the query unless the user has said 'Done' with formulating."
|
114 |
),
|
@@ -120,21 +126,21 @@ def build_agent(session_id:str,message:str) -> Agent:
|
|
120 |
),
|
121 |
Rule(
|
122 |
value="If the user says they want to start over, then you must delete the conversation memory file."
|
123 |
-
)
|
124 |
-
]
|
125 |
)
|
126 |
|
127 |
-
print("Base URL", os.environ.get("BASE_URL","https://cloud.griptape.ai"))
|
128 |
|
129 |
query_client = StructureRunClient(
|
130 |
name="QueryResumeSearcher",
|
131 |
description="Use it to search for a candidate with the query.",
|
132 |
-
driver
|
133 |
-
#base_url=os.environ.get("BASE_URL","https://cloud.griptape.ai"),
|
134 |
structure_id=os.getenv("GT_STRUCTURE_ID"),
|
135 |
api_key=os.getenv("GT_CLOUD_API_KEY"),
|
136 |
structure_run_wait_time_interval=5,
|
137 |
-
structure_run_max_wait_time_attempts=30
|
138 |
),
|
139 |
)
|
140 |
|
@@ -142,36 +148,34 @@ def build_agent(session_id:str,message:str) -> Agent:
|
|
142 |
name="FormulateQueryFromUser",
|
143 |
description="Used to formulate a query from the user's input.",
|
144 |
driver=LocalStructureRunDriver(
|
145 |
-
structure_factory_fn=lambda: build_talk_agent(session_id,message),
|
146 |
-
)
|
147 |
)
|
148 |
-
|
149 |
return Agent(
|
150 |
-
config=
|
151 |
conversation_memory=ConversationMemory(
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
tools=[talk_client,query_client],
|
156 |
rulesets=[ruleset],
|
157 |
)
|
158 |
|
159 |
-
|
160 |
-
|
|
|
161 |
os.remove(file)
|
162 |
|
163 |
|
164 |
-
def send_message(message:str, history, request:gr.Request) -> Any:
|
165 |
if request:
|
166 |
session_hash = request.session_hash
|
167 |
-
agent = build_agent(session_hash,message)
|
168 |
response = agent.run(message)
|
169 |
-
#if re.search(r'\bdone[.,!?]?\b', message, re.IGNORECASE):
|
170 |
-
|
171 |
return response.output.value
|
172 |
|
173 |
-
demo = gr.ChatInterface(
|
174 |
-
fn=send_message
|
175 |
-
)
|
176 |
-
demo.launch()
|
177 |
|
|
|
|
|
|
4 |
from dotenv import load_dotenv
|
5 |
from griptape.structures import Agent
|
6 |
from griptape.tasks import PromptTask
|
7 |
+
from griptape.drivers import (
|
8 |
+
LocalConversationMemoryDriver,
|
9 |
+
GriptapeCloudStructureRunDriver,
|
10 |
+
LocalFileManagerDriver,
|
11 |
+
LocalStructureRunDriver,
|
12 |
+
)
|
13 |
from griptape.memory.structure import ConversationMemory
|
14 |
from griptape.tools import StructureRunClient, FileManager
|
15 |
from griptape.rules import Rule, Ruleset
|
|
|
19 |
import re
|
20 |
|
21 |
|
22 |
+
# Load environment variables
|
|
|
23 |
load_dotenv()
|
24 |
|
25 |
|
26 |
+
# Create an agent that will create a prompt that can be used as input for the query agent from the Griptape Cloud.
|
27 |
+
|
28 |
|
29 |
+
# Function that logs user history - adds to history parameter of Gradio
|
30 |
+
# TODO: Figure out the exact use of this function
|
31 |
def user(user_message, history):
|
32 |
history.append([user_message, None])
|
33 |
return ("", history)
|
34 |
|
35 |
+
|
36 |
+
# Function that logs bot history - adds to the history parameter of Gradio
|
37 |
+
# TODO: Figure out the exact use of this function
|
38 |
def bot(history):
|
39 |
response = send_message(history[-1][0])
|
40 |
history[-1][1] = ""
|
|
|
46 |
yield history
|
47 |
|
48 |
|
49 |
+
def create_prompt_task(session_id: str, message: str) -> PromptTask:
|
50 |
return PromptTask(
|
51 |
f"""
|
52 |
Re-structure the values from the user's questions: '{message}' and the input value from the conversation memory '{session_id}.json' to fit the following format. Leave out attributes that aren't important to the user:
|
|
|
60 |
past projects: <x>
|
61 |
show reel details: <x>
|
62 |
""",
|
63 |
+
)
|
64 |
+
|
65 |
|
66 |
+
def build_talk_agent(session_id: str, message: str) -> Agent:
|
67 |
ruleset = Ruleset(
|
68 |
name="Local Gradio Agent",
|
69 |
rules=[
|
70 |
Rule(
|
71 |
+
value="You are responsible for structuring a user's questions into a specific format for a query."
|
72 |
),
|
73 |
Rule(
|
74 |
+
value="You ask the user follow-up questions to fill in missing information for the format you are trying to fit."
|
75 |
),
|
76 |
Rule(
|
77 |
value="If the user has no preference for a specific attribute, then you can remove it from the query."
|
|
|
79 |
Rule(
|
80 |
value="Only return the current query structure and any questions to fill in missing information."
|
81 |
),
|
82 |
+
Rule(
|
83 |
+
value="Don't return names or usernames, just return the initials of the responses."
|
84 |
+
),
|
85 |
+
],
|
86 |
)
|
87 |
file_manager_tool = FileManager(
|
88 |
name="FileManager",
|
89 |
file_manager_driver=LocalFileManagerDriver(),
|
90 |
+
off_prompt=False,
|
91 |
)
|
92 |
|
93 |
return Agent(
|
94 |
+
config=AnthropicStructureConfig(),
|
95 |
conversation_memory=ConversationMemory(
|
96 |
+
driver=LocalConversationMemoryDriver(file_path=f"{session_id}.json")
|
97 |
+
),
|
|
|
98 |
tools=[file_manager_tool],
|
99 |
+
tasks=[create_prompt_task(session_id, message)],
|
100 |
rulesets=[ruleset],
|
101 |
)
|
102 |
|
103 |
|
104 |
+
# Creates an agent for each run
|
105 |
+
# The agent uses local memory, which it differentiates between by session_hash.
|
106 |
+
def build_agent(session_id: str, message: str) -> Agent:
|
107 |
|
|
|
|
|
|
|
|
|
108 |
ruleset = Ruleset(
|
109 |
name="Local Gradio Agent",
|
110 |
rules=[
|
111 |
Rule(
|
112 |
+
value="You are responsible for structuring a user's questions into a specific format for a query and then querying."
|
113 |
),
|
114 |
Rule(
|
115 |
value="Only return the result of the query, do not provide additional commentary."
|
116 |
),
|
117 |
+
Rule(value="Only perform one task at a time."),
|
|
|
|
|
118 |
Rule(
|
119 |
value="Do not perform the query unless the user has said 'Done' with formulating."
|
120 |
),
|
|
|
126 |
),
|
127 |
Rule(
|
128 |
value="If the user says they want to start over, then you must delete the conversation memory file."
|
129 |
+
),
|
130 |
+
],
|
131 |
)
|
132 |
|
133 |
+
print("Base URL", os.environ.get("BASE_URL", "https://cloud.griptape.ai"))
|
134 |
|
135 |
query_client = StructureRunClient(
|
136 |
name="QueryResumeSearcher",
|
137 |
description="Use it to search for a candidate with the query.",
|
138 |
+
driver=GriptapeCloudStructureRunDriver(
|
139 |
+
# base_url=os.environ.get("BASE_URL","https://cloud.griptape.ai"),
|
140 |
structure_id=os.getenv("GT_STRUCTURE_ID"),
|
141 |
api_key=os.getenv("GT_CLOUD_API_KEY"),
|
142 |
structure_run_wait_time_interval=5,
|
143 |
+
structure_run_max_wait_time_attempts=30,
|
144 |
),
|
145 |
)
|
146 |
|
|
|
148 |
name="FormulateQueryFromUser",
|
149 |
description="Used to formulate a query from the user's input.",
|
150 |
driver=LocalStructureRunDriver(
|
151 |
+
structure_factory_fn=lambda: build_talk_agent(session_id, message),
|
152 |
+
),
|
153 |
)
|
154 |
+
|
155 |
return Agent(
|
156 |
+
config=AnthropicStructureConfig(),
|
157 |
conversation_memory=ConversationMemory(
|
158 |
+
driver=LocalConversationMemoryDriver(file_path=f"{session_id}.json")
|
159 |
+
),
|
160 |
+
tools=[talk_client, query_client],
|
|
|
161 |
rulesets=[ruleset],
|
162 |
)
|
163 |
|
164 |
+
|
165 |
+
def delete_json(session_id: str) -> None:
|
166 |
+
for file in glob.glob(f"{session_id}.json"):
|
167 |
os.remove(file)
|
168 |
|
169 |
|
170 |
+
def send_message(message: str, history, request: gr.Request) -> Any:
|
171 |
if request:
|
172 |
session_hash = request.session_hash
|
173 |
+
agent = build_agent(session_hash, message)
|
174 |
response = agent.run(message)
|
175 |
+
# if re.search(r'\bdone[.,!?]?\b', message, re.IGNORECASE):
|
176 |
+
# delete_json(session_hash)
|
177 |
return response.output.value
|
178 |
|
|
|
|
|
|
|
|
|
179 |
|
180 |
+
demo = gr.ChatInterface(fn=send_message)
|
181 |
+
demo.launch()
|