Skip to content

Commit

Permalink
openai[patch]: tool use integration test (langchain-ai#19460)
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored and rahul-trip committed Mar 27, 2024
1 parent 766619c commit d8e57e8
Showing 1 changed file with 21 additions and 2 deletions.
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

0 comments on commit d8e57e8

Please sign in to comment.