-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routes): add support for structured ouptuts (#59)
* chore(examples): add langchain-openai example from #58 * chore(types): add `refusal` to choice * chore(deps): add langchain-openai as dev dep for testing * chore(docs): remove not about support for older SDK versions * chore(routes): move beta routes to separate dir * chore(types): add structured output type additions * feat(routes): add beta parsed chat route * feat(examples): add parsed chat completion examples
- Loading branch information
Showing
24 changed files
with
517 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from langchain_openai import ChatOpenAI | ||
from pydantic.v1 import SecretStr | ||
|
||
import openai_responses | ||
from openai_responses import OpenAIMock | ||
|
||
|
||
@openai_responses.mock() | ||
def test_langchain_chat_openai_invoke(openai_mock: OpenAIMock): | ||
openai_mock.chat.completions.create.response = { | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"finish_reason": "stop", | ||
"message": { | ||
"content": "J'adore la programmation.", | ||
"role": "assistant", | ||
}, | ||
} | ||
] | ||
} | ||
|
||
llm = ChatOpenAI( | ||
name="My Custom Chatbot", | ||
model="gpt-4o", | ||
temperature=0, | ||
max_tokens=None, | ||
timeout=None, | ||
max_retries=2, | ||
api_key=SecretStr("sk-fake123"), | ||
) | ||
|
||
messages = [ | ||
( | ||
"system", | ||
"You are a helpful assistant that translates English to French. Translate the user sentence.", | ||
), | ||
("human", "I love programming."), | ||
] | ||
ai_msg = llm.invoke(messages) | ||
assert ai_msg.content == "J'adore la programmation." # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from typing import Optional | ||
from datetime import datetime | ||
|
||
import openai | ||
from pydantic import BaseModel | ||
|
||
import openai_responses | ||
from openai_responses import OpenAIMock | ||
|
||
|
||
class CalendarEvent(BaseModel): | ||
name: str | ||
date: str | ||
participants: list[str] | ||
|
||
|
||
@openai_responses.mock() | ||
def test_create_parsed_chat_completion_with_response_format(openai_mock: OpenAIMock): | ||
openai_mock.beta.chat.completions.create.response = { | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"finish_reason": "stop", | ||
"message": { | ||
"content": CalendarEvent( | ||
name="Example Event", | ||
date=datetime.now().strftime("%Y-%m-%d"), | ||
participants=[ | ||
"Alice", | ||
"Bob", | ||
], | ||
).model_dump_json(), | ||
"role": "assistant", | ||
}, | ||
} | ||
] | ||
} | ||
|
||
client = openai.Client(api_key="sk-fake123") | ||
|
||
completion = client.beta.chat.completions.parse( | ||
model="gpt-4o-2024-08-06", | ||
messages=[ | ||
{"role": "system", "content": "Extract the event information."}, | ||
{ | ||
"role": "user", | ||
"content": "Alice and Bob are going to a science fair on today.", | ||
}, | ||
], | ||
response_format=CalendarEvent, | ||
) | ||
|
||
event = completion.choices[0].message.parsed | ||
assert event | ||
assert event.name == "Example Event" | ||
assert datetime.strptime(event.date, "%Y-%m-%d").date() == datetime.now().date() | ||
assert len(event.participants) == 2 | ||
|
||
|
||
@openai_responses.mock() | ||
def test_create_parsed_chat_completion_with_tools(openai_mock: OpenAIMock): | ||
openai_mock.beta.chat.completions.create.response = { | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"finish_reason": "stop", | ||
"message": { | ||
"role": "assistant", | ||
"tool_calls": [ | ||
{ | ||
"id": "call_abc123", | ||
"type": "function", | ||
"function": { | ||
"arguments": CalendarEvent( | ||
name="Example Event", | ||
date=datetime.now().strftime("%Y-%m-%d"), | ||
participants=[ | ||
"Alice", | ||
"Bob", | ||
], | ||
).model_dump_json(), | ||
"name": "CalendarEvent", | ||
}, | ||
} | ||
], | ||
}, | ||
} | ||
] | ||
} | ||
|
||
client = openai.Client(api_key="sk-fake123") | ||
|
||
completion = client.beta.chat.completions.parse( | ||
model="gpt-4o-2024-08-06", | ||
messages=[ | ||
{"role": "system", "content": "Extract the event information."}, | ||
{ | ||
"role": "user", | ||
"content": "Alice and Bob are going to a science fair on today.", | ||
}, | ||
], | ||
tools=[openai.pydantic_function_tool(CalendarEvent)], | ||
) | ||
|
||
event: Optional[CalendarEvent] = ( | ||
completion.choices[0].message.tool_calls[0].function.parsed_arguments # type: ignore | ||
) | ||
|
||
assert event | ||
assert event.name == "Example Event" | ||
assert datetime.strptime(event.date, "%Y-%m-%d").date() == datetime.now().date() | ||
assert len(event.participants) == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.