-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
assistant_server_openai.py
52 lines (43 loc) · 1.4 KB
/
assistant_server_openai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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_openai.my_agent_factory import (
MyAgentFactory,
)
_ = 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: MyAgentFactory()
)
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-assistant",
)
if __name__ == "__main__":
uvicorn.run(app, host="localhost")