Skip to content

Commit 679061e

Browse files
eobdouglas-reid
andauthored
bugfix: Bug in which context_id wasn't stable across REPL messages (#480)
Co-authored-by: Douglas Reid <douglas-reid@users.noreply.github.com>
1 parent ce50e86 commit 679061e

5 files changed

Lines changed: 29 additions & 14 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@ fabric.properties
108108
# Generated website
109109
docs/_build
110110
venv
111+
112+
# Logs directory
111113
logs/

src/steamship/agents/examples/example_assistant.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import uuid
2-
31
from steamship.agents.functional import FunctionsBasedAgent
42
from steamship.agents.llms.openai import ChatOpenAI
53
from steamship.agents.schema.message_selectors import MessageWindowMessageSelector
@@ -31,4 +29,4 @@ def __init__(self, **kwargs):
3129
# AgentREPL provides a mechanism for local execution of an AgentService method.
3230
# This is used for simplified debugging as agents and tools are developed and
3331
# added.
34-
AgentREPL(MyFunctionsBasedAssistant, agent_package_config={}).run(context_id=uuid.uuid4())
32+
AgentREPL(MyFunctionsBasedAssistant, agent_package_config={}).run()

src/steamship/agents/examples/example_react_assistant.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import uuid
2-
31
from steamship.agents.llms.openai import OpenAI
42
from steamship.agents.react import ReACTAgent
53
from steamship.agents.schema.message_selectors import MessageWindowMessageSelector
@@ -33,4 +31,4 @@ def __init__(self, **kwargs):
3331
# AgentREPL provides a mechanism for local execution of an AgentService method.
3432
# This is used for simplified debugging as agents and tools are developed and
3533
# added.
36-
AgentREPL(MyAssistant, agent_package_config={}).run(context_id=uuid.uuid4())
34+
AgentREPL(MyAssistant, agent_package_config={}).run()

src/steamship/agents/service/agent_service.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,21 @@ def run_agent(self, agent: Agent, context: AgentContext):
8888
func(action.output, context.metadata)
8989

9090
@post("prompt")
91-
def prompt(self, prompt: Optional[str] = None, **kwargs) -> List[Block]:
91+
def prompt(
92+
self, prompt: Optional[str] = None, context_id: Optional[str] = None, **kwargs
93+
) -> List[Block]:
9294
"""Run an agent with the provided text as the input."""
9395
prompt = prompt or kwargs.get("question")
9496

9597
# AgentContexts serve to allow the AgentService to run agents
9698
# with appropriate information about the desired tasking.
97-
# Here, we create a new context on each prompt, and append the
98-
# prompt to the message history stored in the context.
99-
context_id = uuid.uuid4()
100-
context = AgentContext.get_or_create(self.client, {"id": f"{context_id}"})
99+
if context_id is None:
100+
context_id = uuid.uuid4()
101+
logging.warning(
102+
f"No context_id was provided; generated {context_id}. This likely means no conversational history will be present."
103+
)
104+
105+
context = AgentContext.get_or_create(self.client, context_keys={"id": f"{context_id}"})
101106
context.chat_history.append_user_message(prompt)
102107

103108
# Add a default LLM

src/steamship/utils/repl.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import abc
22
import contextlib
33
import logging
4+
import uuid
45
from abc import ABC
56
from json import JSONDecodeError
67
from typing import Any, Dict, List, Optional, Type, Union, cast
@@ -142,6 +143,7 @@ def run(self):
142143
class AgentREPL(SteamshipREPL):
143144
agent_class: Type[AgentService]
144145
agent_instance: Optional[AgentService]
146+
context_id: Optional[str] = None
145147
client = Steamship
146148
config = None
147149

@@ -151,6 +153,7 @@ def __init__(
151153
method: Optional[str] = None,
152154
agent_package_config: Optional[Dict[str, Any]] = None,
153155
client: Optional[Steamship] = None,
156+
context_id: Optional[str] = None,
154157
**kwargs,
155158
):
156159
super().__init__(**kwargs)
@@ -159,6 +162,7 @@ def __init__(
159162
self.client = client or Steamship()
160163
self.config = agent_package_config
161164
self.agent_instance = None
165+
self.context_id = context_id or uuid.uuid4()
162166

163167
def run_with_client(self, client: Steamship, **kwargs):
164168
try:
@@ -178,7 +182,7 @@ def colored(text: str, color: str, **kwargs):
178182

179183
while True:
180184
input_text = input(colored(text="Input: ", color="blue")) # noqa: F821
181-
output = responder(input_text)
185+
output = responder(prompt=input_text, context_id=self.context_id)
182186
self.print_object_or_objects(output)
183187

184188
def run(self, **kwargs):
@@ -192,11 +196,19 @@ class HttpREPL(SteamshipREPL):
192196
prompt_url: Optional[AgentService]
193197
client = Steamship
194198
config = None
199+
context_id: Optional[str] = None
195200

196-
def __init__(self, prompt_url: str, client: Optional[Steamship] = None, **kwargs):
201+
def __init__(
202+
self,
203+
prompt_url: str,
204+
context_id: Optional[str] = None,
205+
client: Optional[Steamship] = None,
206+
**kwargs,
207+
):
197208
super().__init__(**kwargs)
198209
self.prompt_url = prompt_url
199210
self.client = client or Steamship()
211+
self.context_id = context_id or uuid.uuid4()
200212

201213
def run_with_client(self, client: Steamship, **kwargs): # noqa: C901
202214
try:
@@ -212,7 +224,7 @@ def colored(text: str, color: str):
212224
input_text = input(colored(text="Input: ", color="blue")) # noqa: F821
213225
resp = requests.post(
214226
self.prompt_url,
215-
json={"prompt": input_text},
227+
json={"prompt": input_text, "context_id": self.context_id},
216228
headers={
217229
"Content-Type": "application/json",
218230
"Authorization": f"Bearer {self.client.config.api_key.get_secret_value()}",

0 commit comments

Comments
 (0)