mirror of
https://github.com/katanemo/plano.git
synced 2026-07-02 15:51:02 +02:00
Add workflow logic for weather forecast demo (#24)
This commit is contained in:
parent
7ef68eccfb
commit
33f9dd22e6
32 changed files with 1902 additions and 459 deletions
24
chatbot-ui/Dockerfile
Normal file
24
chatbot-ui/Dockerfile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# copied from https://github.com/bergos/embedding-server
|
||||
|
||||
FROM python:3 AS base
|
||||
|
||||
#
|
||||
# builder
|
||||
#
|
||||
FROM base AS builder
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY requirements.txt /src/
|
||||
RUN pip install --prefix=/runtime --force-reinstall -r requirements.txt
|
||||
|
||||
COPY . /src
|
||||
|
||||
FROM python:3-slim AS output
|
||||
|
||||
COPY --from=builder /runtime /usr/local
|
||||
|
||||
COPY /app /app
|
||||
WORKDIR /app
|
||||
|
||||
CMD ["python", "run.py"]
|
||||
81
chatbot-ui/app/run.py
Normal file
81
chatbot-ui/app/run.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import gradio as gr
|
||||
|
||||
import asyncio
|
||||
import httpx
|
||||
import async_timeout
|
||||
|
||||
from loguru import logger
|
||||
from typing import Optional, List
|
||||
from pydantic import BaseModel
|
||||
from dotenv import load_dotenv
|
||||
|
||||
import os
|
||||
load_dotenv()
|
||||
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
CHAT_COMPLETION_ENDPOINT = os.getenv("CHAT_COMPLETION_ENDPOINT", "https://api.openai.com/v1/chat/completions")
|
||||
|
||||
class Message(BaseModel):
|
||||
role: str
|
||||
content: str
|
||||
|
||||
async def make_completion(messages:List[Message], nb_retries:int=3, delay:int=30) -> Optional[str]:
|
||||
"""
|
||||
Sends a request to the ChatGPT API to retrieve a response based on a list of previous messages.
|
||||
"""
|
||||
header = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
||||
}
|
||||
try:
|
||||
async with async_timeout.timeout(delay=delay):
|
||||
async with httpx.AsyncClient(headers=header) as aio_client:
|
||||
counter = 0
|
||||
keep_loop = True
|
||||
while keep_loop:
|
||||
logger.debug(f"Chat/Completions Nb Retries : {counter}")
|
||||
try:
|
||||
resp = await aio_client.post(
|
||||
url = CHAT_COMPLETION_ENDPOINT,
|
||||
json = {
|
||||
"model": "gpt-3.5-turbo",
|
||||
"messages": messages
|
||||
}
|
||||
)
|
||||
logger.debug(f"Status Code : {resp.status_code}")
|
||||
if resp.status_code == 200:
|
||||
return resp.json()["choices"][0]["message"]["content"]
|
||||
else:
|
||||
logger.warning(resp.content)
|
||||
keep_loop = False
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
counter = counter + 1
|
||||
keep_loop = counter < nb_retries
|
||||
except asyncio.TimeoutError as e:
|
||||
logger.error(f"Timeout {delay} seconds !")
|
||||
return None
|
||||
|
||||
async def predict(input, history):
|
||||
"""
|
||||
Predict the response of the chatbot and complete a running list of chat history.
|
||||
"""
|
||||
history.append({"role": "user", "content": input})
|
||||
print(history)
|
||||
response = await make_completion(history)
|
||||
history.append({"role": "assistant", "content": response})
|
||||
messages = [(history[i]["content"], history[i+1]["content"]) for i in range(0, len(history)-1, 2)]
|
||||
return messages, history
|
||||
|
||||
"""
|
||||
Gradio Blocks low-level API that allows to create custom web applications (here our chat app)
|
||||
"""
|
||||
with gr.Blocks() as demo:
|
||||
logger.info("Starting Demo...")
|
||||
chatbot = gr.Chatbot(label="WebGPT")
|
||||
state = gr.State([])
|
||||
with gr.Row():
|
||||
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
|
||||
txt.submit(predict, [txt, state], [chatbot, state])
|
||||
|
||||
demo.launch(server_name="0.0.0.0", server_port=8080)
|
||||
6
chatbot-ui/requirements.txt
Normal file
6
chatbot-ui/requirements.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
gradio==4.39.0
|
||||
async_timeout==4.0.3
|
||||
loguru==0.7.2
|
||||
asyncio==3.4.3
|
||||
httpx==0.27.0
|
||||
python-dotenv==1.0.1
|
||||
Loading…
Add table
Add a link
Reference in a new issue