Skip to content

Commit f659229

Browse files
douglas-reidDouglas Reid
andauthored
fix(agents): ensure proper messaging and tool serialization (#568)
In an investigation into somewhat random duplicate message issues, we uncovered a few small issues that this PR cleans up: - don't repeat the user prompt - use proper JSON serialization of tools - catch improper JSON serialization coming back from OpenAI. Co-authored-by: Douglas Reid <doug@steamship.com>
1 parent b56e7cc commit f659229

4 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/steamship/agents/functional/functions_based.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def build_chat_history_for_tool(self, context: AgentContext) -> List[Block]:
5555
messages_from_memory.extend(context.chat_history.select_messages(self.message_selector))
5656

5757
# de-dupe the messages from memory
58-
ids = []
58+
ids = [context.chat_history.last_user_message.id]
5959
for msg in messages_from_memory:
6060
if msg.id not in ids:
6161
messages.append(msg)

src/steamship/agents/functional/output_parser.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import re
33
import string
4+
from json import JSONDecodeError
45
from typing import Dict, List, Optional
56

67
from steamship import Block, MimeTypes, Steamship
@@ -102,7 +103,11 @@ def _remove_block_suffix(candidate: str) -> str:
102103

103104
def parse(self, text: str, context: AgentContext) -> Action:
104105
if "function_call" in text:
105-
return self._extract_action_from_function_call(text, context)
106+
try:
107+
# catch invalid JSON. If it is not valid JSON, just treat as "regular" message
108+
return self._extract_action_from_function_call(text, context)
109+
except JSONDecodeError:
110+
pass
106111

107112
finish_blocks = FunctionsBasedOutputParser._blocks_from_text(context.client, text)
108113
for finish_block in finish_blocks:

src/steamship/agents/llms/openai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def chat(self, messages: List[Block], tools: Optional[List[Tool]], **kwargs) ->
9595
if len(tools) > 0:
9696
functions = []
9797
for tool in tools:
98-
functions.append(tool.as_openai_function())
98+
functions.append(tool.as_openai_function().dict())
9999
options["functions"] = functions
100100

101101
if "max_tokens" in kwargs:

src/steamship/agents/schema/tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def as_openai_function(self) -> OpenAIFunction:
7878
uuid_property_schema = FunctionProperty(
7979
type=JSONType.string,
8080
description="UUID for a Steamship Block. Used to refer to a non-textual input generated by another "
81-
'function. Example: "c2f6818c-233d-4426-9dc5-f3c28fa33068"',
81+
"function. Example: c2f6818c-233d-4426-9dc5-f3c28fa33068",
8282
)
8383

8484
params = FunctionParameters(

0 commit comments

Comments
 (0)