-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Felix T.J. Dietrich <[email protected]>
- Loading branch information
1 parent
0fb30c7
commit ee0d907
Showing
13 changed files
with
405 additions
and
30 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
12 changes: 10 additions & 2 deletions
12
.github/workflows/black.yml → ...hub/workflows/intelligence-service-qa.yml
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from fastapi import FastAPI, HTTPException | ||
from pydantic import BaseModel | ||
from .model import model | ||
|
||
from .model import start_chat as start_chat_function, chat as chat_function | ||
from typing import Dict, Optional | ||
|
||
app = FastAPI( | ||
title="Hephaestus Intelligence Service API", | ||
|
@@ -10,19 +10,43 @@ | |
contact={"name": "Felix T.J. Dietrich", "email": "[email protected]"}, | ||
) | ||
|
||
# Global dictionary to store conversation states | ||
conversations: Dict[str, dict] = {} | ||
|
||
|
||
class ChatRequest(BaseModel): | ||
message: str | ||
thread_id: Optional[str] = None | ||
|
||
|
||
class ChatResponse(BaseModel): | ||
response: str | ||
thread_id: Optional[str] = None | ||
|
||
|
||
@app.post("/chat", response_model=ChatResponse, summary="Get a response from an LLM to a chat message.") | ||
@app.post( | ||
"/chat", | ||
response_model=ChatResponse, | ||
summary="Start and continue a chat session with an LLM.", | ||
) | ||
async def chat(request: ChatRequest): | ||
try: | ||
response = model.invoke(request.message) | ||
return {"response": response.content} | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e)) | ||
if request.thread_id is None: | ||
# Start a new chat session | ||
result = start_chat_function(request.message) | ||
thread_id = result["thread_id"] | ||
state = result["state"] | ||
response_message = result["response"]["messages"][-1].content | ||
conversations[thread_id] = state | ||
return ChatResponse(thread_id=thread_id, response=response_message) | ||
else: | ||
thread_id = request.thread_id | ||
# Check if the thread_id exists | ||
if thread_id not in conversations: | ||
raise HTTPException(status_code=404, detail="Thread ID not found") | ||
state = conversations[thread_id] | ||
user_input = request.message | ||
result = chat_function(thread_id, user_input, state) | ||
state = result["state"] | ||
response_message = result["response"]["messages"][-1].content | ||
conversations[thread_id] = state | ||
return ChatResponse(response=response_message) |
Oops, something went wrong.