mirror of
https://github.com/katanemo/plano.git
synced 2026-06-26 15:39:40 +02:00
lint + formating with black (#158)
* lint + formating with black * add black as pre commit
This commit is contained in:
parent
498e7f9724
commit
5c4a6bc8ff
22 changed files with 581 additions and 295 deletions
|
|
@ -7,47 +7,53 @@ from dotenv import load_dotenv
|
|||
|
||||
load_dotenv()
|
||||
|
||||
OPENAI_API_KEY=os.getenv("OPENAI_API_KEY")
|
||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
|
||||
CHAT_COMPLETION_ENDPOINT = os.getenv("CHAT_COMPLETION_ENDPOINT")
|
||||
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-3.5-turbo")
|
||||
ARCH_STATE_HEADER = 'x-arch-state'
|
||||
ARCH_STATE_HEADER = "x-arch-state"
|
||||
|
||||
log.info("CHAT_COMPLETION_ENDPOINT: ", CHAT_COMPLETION_ENDPOINT)
|
||||
|
||||
client = OpenAI(api_key=OPENAI_API_KEY, base_url=CHAT_COMPLETION_ENDPOINT, http_client=DefaultHttpxClient(headers={"accept-encoding": "*"}))
|
||||
client = OpenAI(
|
||||
api_key=OPENAI_API_KEY,
|
||||
base_url=CHAT_COMPLETION_ENDPOINT,
|
||||
http_client=DefaultHttpxClient(headers={"accept-encoding": "*"}),
|
||||
)
|
||||
|
||||
|
||||
def predict(message, state):
|
||||
if 'history' not in state:
|
||||
state['history'] = []
|
||||
if "history" not in state:
|
||||
state["history"] = []
|
||||
history = state.get("history")
|
||||
history.append({"role": "user", "content": message})
|
||||
log.info("history: ", history)
|
||||
|
||||
# Custom headers
|
||||
custom_headers = {
|
||||
'x-arch-openai-api-key': f"{OPENAI_API_KEY}",
|
||||
'x-arch-mistral-api-key': f"{MISTRAL_API_KEY}",
|
||||
'x-arch-deterministic-provider': 'openai',
|
||||
"x-arch-openai-api-key": f"{OPENAI_API_KEY}",
|
||||
"x-arch-mistral-api-key": f"{MISTRAL_API_KEY}",
|
||||
"x-arch-deterministic-provider": "openai",
|
||||
}
|
||||
|
||||
metadata = None
|
||||
if 'arch_state' in state:
|
||||
metadata = {ARCH_STATE_HEADER: state['arch_state']}
|
||||
if "arch_state" in state:
|
||||
metadata = {ARCH_STATE_HEADER: state["arch_state"]}
|
||||
|
||||
try:
|
||||
raw_response = client.chat.completions.with_raw_response.create(model=MODEL_NAME,
|
||||
messages = history,
|
||||
temperature=1.0,
|
||||
metadata=metadata,
|
||||
extra_headers=custom_headers
|
||||
)
|
||||
raw_response = client.chat.completions.with_raw_response.create(
|
||||
model=MODEL_NAME,
|
||||
messages=history,
|
||||
temperature=1.0,
|
||||
metadata=metadata,
|
||||
extra_headers=custom_headers,
|
||||
)
|
||||
except Exception as e:
|
||||
log.info(e)
|
||||
# remove last user message in case of exception
|
||||
history.pop()
|
||||
log.info("Error calling gateway API: {}".format(e.message))
|
||||
raise gr.Error("Error calling gateway API: {}".format(e.message))
|
||||
log.info(e)
|
||||
# remove last user message in case of exception
|
||||
history.pop()
|
||||
log.info("Error calling gateway API: {}".format(e.message))
|
||||
raise gr.Error("Error calling gateway API: {}".format(e.message))
|
||||
|
||||
log.info("raw_response: ", raw_response.text)
|
||||
response = raw_response.parse()
|
||||
|
|
@ -57,24 +63,33 @@ def predict(message, state):
|
|||
response_json = json.loads(raw_response.text)
|
||||
arch_state = None
|
||||
if response_json:
|
||||
metadata = response_json.get('metadata', {})
|
||||
if metadata:
|
||||
arch_state = metadata.get(ARCH_STATE_HEADER, None)
|
||||
metadata = response_json.get("metadata", {})
|
||||
if metadata:
|
||||
arch_state = metadata.get(ARCH_STATE_HEADER, None)
|
||||
if arch_state:
|
||||
state['arch_state'] = arch_state
|
||||
state["arch_state"] = arch_state
|
||||
|
||||
content = response.choices[0].message.content
|
||||
|
||||
history.append({"role": "assistant", "content": content, "model": response.model})
|
||||
messages = [(history[i]["content"], history[i+1]["content"]) for i in range(0, len(history)-1, 2)]
|
||||
messages = [
|
||||
(history[i]["content"], history[i + 1]["content"])
|
||||
for i in range(0, len(history) - 1, 2)
|
||||
]
|
||||
return messages, state
|
||||
|
||||
|
||||
with gr.Blocks(fill_height=True, css="footer {visibility: hidden}") as demo:
|
||||
print("Starting Demo...")
|
||||
chatbot = gr.Chatbot(label="Arch Chatbot", scale=1)
|
||||
state = gr.State({})
|
||||
with gr.Row():
|
||||
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter", scale=1, autofocus=True)
|
||||
txt = gr.Textbox(
|
||||
show_label=False,
|
||||
placeholder="Enter text and press enter",
|
||||
scale=1,
|
||||
autofocus=True,
|
||||
)
|
||||
|
||||
txt.submit(predict, [txt, state], [chatbot, state])
|
||||
|
||||
|
|
|
|||
|
|
@ -5,26 +5,32 @@ from openai import OpenAI
|
|||
import gradio as gr
|
||||
|
||||
api_key = os.getenv("OPENAI_API_KEY")
|
||||
CHAT_COMPLETION_ENDPOINT = os.getenv("CHAT_COMPLETION_ENDPOINT", "https://api.openai.com/v1")
|
||||
CHAT_COMPLETION_ENDPOINT = os.getenv(
|
||||
"CHAT_COMPLETION_ENDPOINT", "https://api.openai.com/v1"
|
||||
)
|
||||
|
||||
client = OpenAI(api_key=api_key, base_url=CHAT_COMPLETION_ENDPOINT)
|
||||
|
||||
|
||||
def predict(message, history):
|
||||
history_openai_format = []
|
||||
for human, assistant in history:
|
||||
history_openai_format.append({"role": "user", "content": human })
|
||||
history_openai_format.append({"role": "assistant", "content":assistant})
|
||||
history_openai_format.append({"role": "user", "content": human})
|
||||
history_openai_format.append({"role": "assistant", "content": assistant})
|
||||
history_openai_format.append({"role": "user", "content": message})
|
||||
|
||||
response = client.chat.completions.create(model='gpt-3.5-turbo',
|
||||
messages= history_openai_format,
|
||||
temperature=1.0,
|
||||
stream=True)
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=history_openai_format,
|
||||
temperature=1.0,
|
||||
stream=True,
|
||||
)
|
||||
|
||||
partial_message = ""
|
||||
for chunk in response:
|
||||
if chunk.choices[0].delta.content is not None:
|
||||
partial_message = partial_message + chunk.choices[0].delta.content
|
||||
yield partial_message
|
||||
partial_message = partial_message + chunk.choices[0].delta.content
|
||||
yield partial_message
|
||||
|
||||
|
||||
gr.ChatInterface(predict).launch(server_name="0.0.0.0", server_port=8081)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue