-
Notifications
You must be signed in to change notification settings - Fork 77
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
1 parent
2f2bf62
commit 1435bac
Showing
19 changed files
with
2,006 additions
and
170 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import functools | ||
from typing import List, Literal | ||
|
||
import pydantic | ||
from fastapi import FastAPI | ||
from starlette.requests import Request | ||
|
||
import burr | ||
from burr.core import Application, ApplicationBuilder, State | ||
from burr.tracking import LocalTrackingClient | ||
|
||
from examples.gpt import application as chat_application | ||
|
||
PROJECT_ID = "demo:chatbot" | ||
|
||
|
||
class ChatItem(pydantic.BaseModel): | ||
content: str | ||
type: Literal["image", "text", "code"] | ||
role: Literal["user", "assistant"] | ||
|
||
|
||
@functools.lru_cache(maxsize=128) | ||
def _get_application(project_id: str, app_id: str) -> Application: | ||
app = chat_application.application(use_hamilton=False, app_id=app_id, project_id="demo:chatbot") | ||
if LocalTrackingClient.app_log_exists(project_id, app_id): | ||
state, _ = LocalTrackingClient.load_state(project_id, app_id) # TODO -- handle entrypoint | ||
app.update_state( | ||
State(state) | ||
) # TODO -- handle the entrypoint -- this will always reset to prompt | ||
return app | ||
|
||
|
||
def chat_response(project_id: str, app_id: str, prompt: str) -> List[ChatItem]: | ||
"""Chat response endpoint. | ||
:param project_id: Project ID to run | ||
:param app_id: Application ID to run | ||
:param prompt: Prompt to send to the chatbot | ||
:return: | ||
""" | ||
burr_app = _get_application(project_id, app_id) | ||
_, _, state = burr_app.run(halt_after=["response"], inputs=dict(prompt=prompt)) | ||
return state.get("chat_history", []) | ||
|
||
|
||
def chat_history(project_id: str, app_id: str) -> List[ChatItem]: | ||
burr_app = _get_application(project_id, app_id) | ||
state = burr_app.state | ||
print(state) | ||
return state.get("chat_history", []) | ||
|
||
|
||
def register(app: FastAPI, api_prefix: str): | ||
app.post(f"{api_prefix}/{{project_id}}/{{app_id}}/response", response_model=List[ChatItem])( | ||
chat_response | ||
) | ||
app.get(f"{api_prefix}/{{project_id}}/{{app_id}}/history", response_model=List[ChatItem])( | ||
chat_history | ||
) |
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
Oops, something went wrong.