Create get_fn.py
Browse files
get_fn.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import base64
|
3 |
+
from openai import OpenAI
|
4 |
+
import gradio as gr
|
5 |
+
from typing import Callable
|
6 |
+
|
7 |
+
def get_fn(model_name: str, preprocess: Callable, postprocess: Callable, api_key: str):
|
8 |
+
def fn(message, history):
|
9 |
+
inputs = preprocess(message, history)
|
10 |
+
client = OpenAI(
|
11 |
+
base_url="https://api.sambanova.ai/v1/",
|
12 |
+
api_key=api_key,
|
13 |
+
)
|
14 |
+
try:
|
15 |
+
completion = client.chat.completions.create(
|
16 |
+
model=model_name,
|
17 |
+
messages=inputs["messages"],
|
18 |
+
stream=True,
|
19 |
+
)
|
20 |
+
response_text = ""
|
21 |
+
for chunk in completion:
|
22 |
+
delta = chunk.choices[0].delta.content or ""
|
23 |
+
response_text += delta
|
24 |
+
yield postprocess(response_text)
|
25 |
+
except Exception as e:
|
26 |
+
error_message = f"Error: {str(e)}"
|
27 |
+
return error_message
|
28 |
+
|
29 |
+
return fn
|