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

[langchain] fix OpenAIAssistantRunnable.create_assistant #19081

Merged
merged 9 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 28 additions & 3 deletions libs/langchain/langchain/agents/openai_assistant/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
import json
from json import JSONDecodeError
from time import sleep
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple, Union
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
List,
Optional,
Sequence,
Tuple,
Type,
Union,
)

from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.callbacks import CallbackManager
from langchain_core.load import dumpd
from langchain_core.pydantic_v1 import Field, root_validator
from langchain_core.pydantic_v1 import BaseModel, Field, root_validator
from langchain_core.runnables import RunnableConfig, RunnableSerializable, ensure_config
from langchain_core.tools import BaseTool
from langchain_core.utils.function_calling import convert_to_openai_tool
Expand Down Expand Up @@ -76,6 +87,20 @@ def _get_openai_async_client() -> openai.AsyncOpenAI:
) from e


def _get_assistants_tool(
tool: Union[Dict[str, Any], Type[BaseModel], Callable, BaseTool],
) -> Dict[str, Any]:
"""Convert a raw function/class to an OpenAI tool.

Note that OpenAI assistants supports several built-in tools,
such as "code_interpreter" and "retrieval."
"""
if isinstance(tool, dict) and "type" in tool:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to support passing in non-openai format json schema? if so we should explicitly check that tool["type"] is code_interpreter or retrieval

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also see an argument that we should add that check to convert_to_openai_tool

return tool
else:
return convert_to_openai_tool(tool)


OutputType = Union[
List[OpenAIAssistantAction],
OpenAIAssistantFinish,
Expand Down Expand Up @@ -210,7 +235,7 @@ def create_assistant(
assistant = client.beta.assistants.create(
name=name,
instructions=instructions,
tools=[convert_to_openai_tool(tool) for tool in tools], # type: ignore
tools=[_get_assistants_tool(tool) for tool in tools], # type: ignore
model=model,
)
return cls(assistant_id=assistant.id, client=client, **kwargs)
Expand Down
24 changes: 24 additions & 0 deletions libs/langchain/tests/unit_tests/agents/test_openai_assistant.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
from typing import Any
from unittest.mock import MagicMock, patch

import pytest

from langchain.agents.openai_assistant import OpenAIAssistantRunnable


def _create_mock_client(*args: Any, **kwargs: Any) -> Any:
client = MagicMock()
client.beta.assistants.create().id = "abc123"
return client


@pytest.mark.requires("openai")
def test_user_supplied_client() -> None:
import openai
Expand All @@ -19,3 +28,18 @@ def test_user_supplied_client() -> None:
)

assert assistant.client == client


@pytest.mark.requires("openai")
@patch(
"langchain.agents.openai_assistant.base._get_openai_client",
new=_create_mock_client,
)
def test_create_assistant() -> None:
assistant = OpenAIAssistantRunnable.create_assistant(
name="name",
instructions="instructions",
tools=[{"type": "code_interpreter"}],
model="",
)
assert isinstance(assistant, OpenAIAssistantRunnable)