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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

openai[patch]: tool use integration test #19460

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test ChatOpenAI chat model."""

from typing import Any, Optional, cast
from typing import Any, List, Optional, cast

import pytest
from langchain_core.callbacks import CallbackManager
Expand All @@ -10,6 +9,7 @@
BaseMessageChunk,
HumanMessage,
SystemMessage,
ToolMessage,
)
from langchain_core.outputs import (
ChatGeneration,
Expand Down Expand Up @@ -470,6 +470,25 @@ async def test_async_response_metadata_streaming() -> None:
assert "content" in cast(BaseMessageChunk, full).response_metadata["logprobs"]


class GenerateUsername(BaseModel):
"Get a username based on someone's name and hair color."

name: str
hair_color: str


def test_tool_use() -> None:
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
llm_with_tool = llm.bind_tools(tools=[GenerateUsername], tool_choice=True)
msgs: List = [HumanMessage("Sally has green hair, what would her username be?")]
ai_msg = llm_with_tool.invoke(msgs)
tool_msg = ToolMessage(
"sally_green_hair", tool_call_id=ai_msg.additional_kwargs["tool_calls"][0]["id"]
)
msgs.extend([ai_msg, tool_msg])
llm_with_tool.invoke(msgs)


def test_openai_structured_output() -> None:
class MyModel(BaseModel):
"""A Person"""
Expand Down