-
-
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.
- Loading branch information
Showing
11 changed files
with
260 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# For functional testing | ||
OPENAI_API_KEY="" | ||
ANTHROPIC_API_KEY="" | ||
ANTHROPIC_API_KEY="" | ||
GROQ_API_KEY="" |
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
19 changes: 19 additions & 0 deletions
19
...hain_openai_api_bridge/chat_model_adapter/default_openai_compatible_chat_model_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,19 @@ | ||
from abc import abstractmethod | ||
from typing import List, Union | ||
from langchain_core.messages import BaseMessage | ||
|
||
from langchain_openai_api_bridge.chat_model_adapter.base_openai_compatible_chat_model_adapter import ( | ||
BaseOpenAICompatibleChatModelAdapter, | ||
) | ||
|
||
|
||
class DefaultOpenAICompatibleChatModelAdapter(BaseOpenAICompatibleChatModelAdapter): | ||
|
||
@abstractmethod | ||
def is_compatible(self, llm_type: str): | ||
return True | ||
|
||
def to_openai_format_messages( | ||
self, messages: Union[List[BaseMessage], List[List[BaseMessage]]] | ||
): | ||
return messages |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
21 changes: 21 additions & 0 deletions
21
tests/test_functional/fastapi_assistant_agent_groq/README.md
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,21 @@ | ||
# Expose as an OpenAI API Groq assistant (Assistant API) | ||
|
||
:warning: Groq does not support streaming with tools. Make sure to set `streaming=False,` | ||
|
||
```python | ||
chat_model = ChatGroq( | ||
model="llama3-8b-8192", | ||
streaming=False, # <<--- Must be set to False when used with LangGraph / Tools | ||
) | ||
``` | ||
|
||
:warning: Note that Groq models do not currently support multi-modal capabilities. Do not use payload with image reference | ||
|
||
```python | ||
{ | ||
"type": "image_url", | ||
"image_url": { | ||
"url": "data:image/jpeg;base64,iVBORw0KGgo=" | ||
}, | ||
}, | ||
``` |
52 changes: 52 additions & 0 deletions
52
tests/test_functional/fastapi_assistant_agent_groq/assistant_server_groq.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,52 @@ | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi import FastAPI | ||
from dotenv import load_dotenv, find_dotenv | ||
import uvicorn | ||
|
||
from langchain_openai_api_bridge.assistant import ( | ||
InMemoryMessageRepository, | ||
InMemoryRunRepository, | ||
InMemoryThreadRepository, | ||
) | ||
from langchain_openai_api_bridge.fastapi.langchain_openai_api_bridge_fastapi import ( | ||
LangchainOpenaiApiBridgeFastAPI, | ||
) | ||
from tests.test_functional.fastapi_assistant_agent_groq.my_groq_agent_factory import ( | ||
MyGroqAgentFactory, | ||
) | ||
|
||
_ = 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=["*"], | ||
) | ||
|
||
in_memory_thread_repository = InMemoryThreadRepository() | ||
in_memory_message_repository = InMemoryMessageRepository() | ||
in_memory_run_repository = InMemoryRunRepository() | ||
|
||
bridge = LangchainOpenaiApiBridgeFastAPI( | ||
app=app, agent_factory_provider=lambda: MyGroqAgentFactory() | ||
) | ||
bridge.bind_openai_assistant_api( | ||
thread_repository_provider=in_memory_thread_repository, | ||
message_repository_provider=in_memory_message_repository, | ||
run_repository_provider=in_memory_run_repository, | ||
prefix="/my-groq-assistant", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="localhost") |
35 changes: 35 additions & 0 deletions
35
tests/test_functional/fastapi_assistant_agent_groq/my_groq_agent_factory.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,35 @@ | ||
from langchain_groq import ChatGroq | ||
from langchain_openai_api_bridge.chat_model_adapter import ( | ||
OpenAICompatibleChatModel, | ||
) | ||
from langchain_openai_api_bridge.core.agent_factory import AgentFactory | ||
from langgraph.graph.graph import CompiledGraph | ||
from langchain_core.language_models import BaseChatModel | ||
from langchain_core.tools import tool | ||
from langgraph.prebuilt import create_react_agent | ||
|
||
from langchain_openai_api_bridge.core.create_agent_dto import CreateAgentDto | ||
|
||
|
||
@tool | ||
def magic_number_tool(input: int) -> int: | ||
"""Applies a magic function to an input.""" | ||
return input + 2 | ||
|
||
|
||
class MyGroqAgentFactory(AgentFactory): | ||
|
||
def create_agent(self, llm: BaseChatModel, dto: CreateAgentDto) -> CompiledGraph: | ||
return create_react_agent( | ||
llm, | ||
[magic_number_tool], | ||
messages_modifier="""You are a helpful assistant.""", | ||
) | ||
|
||
def create_llm(self, dto: CreateAgentDto) -> CompiledGraph: | ||
chat_model = ChatGroq( | ||
model=dto.model, | ||
streaming=False, | ||
) | ||
|
||
return OpenAICompatibleChatModel(chat_model=chat_model) |
47 changes: 47 additions & 0 deletions
47
tests/test_functional/fastapi_assistant_agent_groq/test_assistant_server_groq.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,47 @@ | ||
import pytest | ||
from openai import OpenAI | ||
|
||
from fastapi.testclient import TestClient | ||
from assistant_server_groq import app | ||
from tests.test_functional.assistant_stream_utils import ( | ||
assistant_stream_events_to_str_response, | ||
) | ||
|
||
|
||
test_api = TestClient(app) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def openai_client(): | ||
return OpenAI( | ||
base_url="http://testserver/my-groq-assistant/openai/v1", | ||
http_client=test_api, | ||
) | ||
|
||
|
||
class TestGroqAssistant: | ||
|
||
def test_run_stream_message_deltas( | ||
self, | ||
openai_client: OpenAI, | ||
): | ||
thread = openai_client.beta.threads.create( | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Hello!", | ||
}, | ||
] | ||
) | ||
|
||
stream = openai_client.beta.threads.runs.create( | ||
thread_id=thread.id, | ||
model="llama3-8b-8192", | ||
assistant_id="any", | ||
stream=True, | ||
temperature=0, | ||
) | ||
|
||
str_response = assistant_stream_events_to_str_response(stream) | ||
|
||
assert len(str_response) > 0 |