Skip to content

Commit

Permalink
fix: add thought for GuiAgentInterpreterChatResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu007 committed Aug 24, 2024
1 parent 60f0e40 commit 0044946
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/codeinterpreterapi/crew/custom_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(self, agent_executor: Any, **data):

def interpolate_inputs(self, inputs: Dict[str, Any]) -> None:
"""Interpolate inputs into the task description and expected output."""
print("interpolate_inputs inputs=", inputs)
super().interpolate_inputs(inputs)

def execute_task(self, task: Any, context: Optional[str] = None, tools: Optional[List[Any]] = None) -> str:
Expand All @@ -45,6 +44,7 @@ def execute_task(self, task: Any, context: Optional[str] = None, tools: Optional
input_dict["message"] = "タスクを実行してください。\n" + task.expected_output
result = self.agent_executor.invoke(input=input_dict)
result_str = MultiConverter.to_str(result)
print("execute_task result(type)=", type(result_str))
print("execute_task result=", result_str)

# TODO: return full dict when crewai is updated
Expand Down
3 changes: 2 additions & 1 deletion src/codeinterpreterapi/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ class CodeInterpreterResponse(AIMessage):

files: Optional[list[File]] = []
code_log: Optional[dict[str, str]] = []
agent_name: str = ""
agent_name: Optional[str] = ""
thought: Optional[str] = "" # 中間的な思考

def show(self) -> None:
print("AI: ", self.content)
Expand Down
2 changes: 1 addition & 1 deletion src/codeinterpreterapi/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def generate_response_stream(
self,
user_msg: BaseMessageContent,
files: list[File] = None,
) -> Iterator[str]:
) -> Iterator[CodeInterpreterResponse]:
"""Generate a Code Interpreter response based on the user's input."""
if files is None:
files = []
Expand Down
19 changes: 17 additions & 2 deletions src/codeinterpreterapi/utils/multi_converter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Dict, List

from crewai.crews.crew_output import CrewOutput, TaskOutput
from langchain_core.messages import AIMessageChunk


Expand All @@ -14,13 +15,16 @@ def to_str(input_obj: Any) -> str:
if len(input_obj) > 0:
input_obj = MultiConverter._process_dict(input_obj[-1])
else:
input_obj = ""
return "no output"
elif isinstance(input_obj, Dict):
input_obj = MultiConverter._process_dict(input_obj)
elif isinstance(input_obj, CrewOutput):
input_obj = MultiConverter._process_crew_output(input_obj)
else:
print("MultiConverter to_str type(input_obj)=", type(input_obj))
return str(input_obj)

# 再帰
# 確実にstr以外は念のため再帰
return MultiConverter.to_str(input_obj)

@staticmethod
Expand All @@ -43,3 +47,14 @@ def _process_dict(input_dict: Dict[str, Any]) -> str:
keys = ["tool", "tool_input_obj", "log"]
code_log_item = {key: str(input_dict[key]) for key in keys if key in input_dict}
return str(code_log_item) if code_log_item else str(input_dict)

@staticmethod
def _process_crew_output(input_crew_output: CrewOutput) -> str:
# TODO: return json or
last_task_output: TaskOutput = input_crew_output.tasks_output[-1]
if last_task_output.json_dict:
return str(last_task_output.json_dict)
elif last_task_output.pydantic:
return str(last_task_output.pydantic)
else:
return last_task_output.raw

0 comments on commit 0044946

Please sign in to comment.