Skip to content

Commit ec1d7a6

Browse files
authored
feat: pass threadId on agent build request (#30)
1 parent 7d9b387 commit ec1d7a6

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

langchain_openai_api_bridge/core/create_agent_dto.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ class CreateAgentDto(BaseModel):
88
temperature: float = 0.7
99
max_tokens: Optional[int] = None
1010
assistant_id: Optional[str] = ""
11+
thread_id: Optional[str] = ""

langchain_openai_api_bridge/fastapi/assistant_api_router.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
from langchain_openai_api_bridge.assistant.repository.run_repository import (
2424
RunRepository,
2525
)
26-
from langchain_openai_api_bridge.core.agent_factory import AgentFactory
27-
from langchain_openai_api_bridge.core.create_agent_dto import CreateAgentDto
26+
from langchain_openai_api_bridge.fastapi.internal_agent_factory import (
27+
InternalAgentFactory,
28+
)
2829
from langchain_openai_api_bridge.core.utils.tiny_di_container import TinyDIContainer
2930
from langchain_openai_api_bridge.fastapi.token_getter import get_bearer_token
3031

@@ -117,16 +118,10 @@ async def assistant_create_thread_runs(
117118
thread_run_dto.thread_id = thread_id
118119

119120
api_key = get_bearer_token(authorization)
120-
121-
agent_factory = tiny_di_container.resolve(AgentFactory)
122-
create_agent_dto = CreateAgentDto(
123-
model=thread_run_dto.model,
124-
api_key=api_key,
125-
temperature=thread_run_dto.temperature,
126-
assistant_id=thread_run_dto.assistant_id,
121+
agent_factory = tiny_di_container.resolve(InternalAgentFactory)
122+
agent = agent_factory.create_agent(
123+
thread_run_dto=thread_run_dto, api_key=api_key
127124
)
128-
llm = agent_factory.create_llm(dto=create_agent_dto)
129-
agent = agent_factory.create_agent(llm=llm, dto=create_agent_dto)
130125

131126
service = tiny_di_container.resolve(AssistantRunService)
132127

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from langchain_openai_api_bridge.assistant.create_thread_runs_api_dto import (
2+
ThreadRunsDto,
3+
)
4+
from langchain_openai_api_bridge.core.agent_factory import AgentFactory
5+
from langchain_openai_api_bridge.core.create_agent_dto import CreateAgentDto
6+
from langchain_core.runnables import Runnable
7+
8+
9+
class InternalAgentFactory:
10+
def __init__(self, agent_factory: AgentFactory) -> None:
11+
self.agent_factory = agent_factory
12+
13+
def create_agent(self, thread_run_dto: ThreadRunsDto, api_key: str) -> Runnable:
14+
create_agent_dto = CreateAgentDto(
15+
model=thread_run_dto.model,
16+
thread_id=thread_run_dto.thread_id,
17+
api_key=api_key,
18+
temperature=thread_run_dto.temperature,
19+
assistant_id=thread_run_dto.assistant_id,
20+
)
21+
llm = self.agent_factory.create_llm(dto=create_agent_dto)
22+
return self.agent_factory.create_agent(llm=llm, dto=create_agent_dto)

langchain_openai_api_bridge/fastapi/langchain_openai_api_bridge_fastapi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
from langchain_openai_api_bridge.fastapi.chat_completion_router import (
3333
create_openai_chat_completion_router,
3434
)
35+
from langchain_openai_api_bridge.fastapi.internal_agent_factory import (
36+
InternalAgentFactory,
37+
)
3538

3639

3740
class LangchainOpenaiApiBridgeFastAPI(LangchainOpenaiApiBridge):
@@ -79,6 +82,8 @@ def bind_openai_assistant_api(
7982
self.tiny_di_container.register(RunRepository, to=run_repository_provider)
8083
register_assistant_adapter(self.tiny_di_container)
8184

85+
self.tiny_di_container.register(InternalAgentFactory)
86+
8287
assistant_router = create_openai_assistant_router(
8388
tiny_di_container=self.tiny_di_container,
8489
prefix=prefix,

0 commit comments

Comments
 (0)