-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of custom event handler support (#42)
- Loading branch information
Showing
7 changed files
with
161 additions
and
19 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
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
55 changes: 55 additions & 0 deletions
55
tests/test_functional/fastapi_chat_completion_openai/server_openai_event_adapter.py
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,55 @@ | ||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from dotenv import load_dotenv, find_dotenv | ||
import uvicorn | ||
|
||
from langchain_openai_api_bridge.core.create_agent_dto import CreateAgentDto | ||
from langchain_openai_api_bridge.fastapi.langchain_openai_api_bridge_fastapi import ( | ||
LangchainOpenaiApiBridgeFastAPI, | ||
) | ||
from langchain_openai import ChatOpenAI | ||
|
||
_ = load_dotenv(find_dotenv()) | ||
|
||
|
||
app = FastAPI( | ||
title="Langchain Agent OpenAI API Bridge", | ||
version="1.0", | ||
description="OpenAI API exposing langchain agent", | ||
) | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
expose_headers=["*"], | ||
) | ||
|
||
|
||
def create_agent(dto: CreateAgentDto): | ||
return ChatOpenAI( | ||
temperature=dto.temperature or 0.7, | ||
model=dto.model, | ||
max_tokens=dto.max_tokens, | ||
api_key=dto.api_key, | ||
) | ||
|
||
|
||
bridge = LangchainOpenaiApiBridgeFastAPI(app=app, agent_factory_provider=create_agent) | ||
|
||
|
||
def event_adapter(event): | ||
kind = event["event"] | ||
match kind: | ||
case "on_chat_model_stream": | ||
return event | ||
|
||
|
||
bridge.bind_openai_chat_completion( | ||
prefix="/my-custom-events-path", event_adapter=event_adapter | ||
) | ||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="localhost") |
44 changes: 44 additions & 0 deletions
44
tests/test_functional/fastapi_chat_completion_openai/test_server_openai_event_adapter.py
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,44 @@ | ||
import pytest | ||
from openai import OpenAI | ||
from fastapi.testclient import TestClient | ||
from server_openai_event_adapter import app | ||
|
||
|
||
test_api = TestClient(app) | ||
|
||
|
||
@pytest.fixture | ||
def openai_client_custom_events(): | ||
return OpenAI( | ||
base_url="http://testserver/my-custom-events-path/openai/v1", | ||
http_client=test_api, | ||
) | ||
|
||
|
||
def test_chat_completion_invoke_custom_events(openai_client_custom_events): | ||
chat_completion = openai_client_custom_events.chat.completions.create( | ||
model="gpt-4o-mini", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": 'Say "This is a test"', | ||
} | ||
], | ||
) | ||
assert "This is a test" in chat_completion.choices[0].message.content | ||
|
||
|
||
def test_chat_completion_stream_custom_events(openai_client_custom_events): | ||
chunks = openai_client_custom_events.chat.completions.create( | ||
model="gpt-4o-mini", | ||
messages=[{"role": "user", "content": 'Say "This is a test"'}], | ||
stream=True, | ||
) | ||
every_content = [] | ||
for chunk in chunks: | ||
if chunk.choices and isinstance(chunk.choices[0].delta.content, str): | ||
every_content.append(chunk.choices[0].delta.content) | ||
|
||
stream_output = "".join(every_content) | ||
|
||
assert "This is a test" in stream_output |
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