Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix output_prefix in do() method for ChatGPT Agent #2457

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions flytekit/extend/backend/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ class SyncAgentBase(AgentBase):
name = "Base Sync Agent"

@abstractmethod
def do(self, task_template: TaskTemplate, inputs: Optional[LiteralMap], output_prefix: str, **kwargs) -> Resource:
def do(
self, task_template: TaskTemplate, output_prefix: str, inputs: Optional[LiteralMap] = None, **kwargs
) -> Resource:
"""
This is the method that the agent will run.
"""
Expand Down Expand Up @@ -247,7 +249,9 @@ def execute(self: PythonTask, **kwargs) -> LiteralMap:

agent = AgentRegistry.get_agent(task_template.type, task_template.task_type_version)

resource = asyncio.run(self._do(agent, task_template, output_prefix, kwargs))
resource = asyncio.run(
self._do(agent=agent, template=task_template, output_prefix=output_prefix, inputs=kwargs)
)
if resource.phase != TaskExecution.SUCCEEDED:
raise FlyteUserException(f"Failed to run the task {self.name} with error: {resource.message}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async def do(
self,
task_template: TaskTemplate,
inputs: Optional[LiteralMap] = None,
**kwargs,
) -> Resource:
ctx = FlyteContextManager.current_context()
input_python_value = TypeEngine.literal_map_to_kwargs(ctx, inputs, {"message": str})
Expand Down
22 changes: 22 additions & 0 deletions plugins/flytekit-openai/tests/chatgpt/test_chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import OrderedDict
from unittest import mock

from flytekitplugins.openai import ChatGPTTask

Expand All @@ -7,6 +8,14 @@
from flytekit.models.types import SimpleType


async def mock_acreate(*args, **kwargs) -> str:
mock_response = mock.MagicMock()
mock_choice = mock.MagicMock()
mock_choice.message.content = "mocked_message"
mock_response.choices = [mock_choice]
return mock_response


def test_chatgpt_task():
chatgpt_task = ChatGPTTask(
name="chatgpt",
Expand Down Expand Up @@ -40,3 +49,16 @@ def test_chatgpt_task():

assert chatgpt_task_spec.template.interface.inputs["message"].type.simple == SimpleType.STRING
assert chatgpt_task_spec.template.interface.outputs["o0"].type.simple == SimpleType.STRING

with mock.patch("openai.resources.chat.completions.AsyncCompletions.create", new=mock_acreate):
chatgpt_task = ChatGPTTask(
name="chatgpt",
openai_organization="TEST ORGANIZATION ID",
chatgpt_config={
"model": "gpt-3.5-turbo",
"temperature": 0.7,
},
)

response = chatgpt_task(message="hi")
assert response == "mocked_message"
Loading