File size: 941 Bytes
a9b015c
 
 
 
 
 
 
 
 
 
b015fe9
a9b015c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import base64
from openai import OpenAI
import gradio as gr
from typing import Callable

def get_fn(model_name: str, preprocess: Callable, postprocess: Callable, api_key: str):
    def fn(message, history):
        inputs = preprocess(message, history)
        client = OpenAI(
            base_url="https://fast-cloud-snova-ai-dev-0-api.cloud.snova.ai/v1",
            api_key=api_key,
        )
        try:
            completion = client.chat.completions.create(
                model=model_name,
                messages=inputs["messages"],
                stream=True,
            )
            response_text = ""
            for chunk in completion:
                delta = chunk.choices[0].delta.content or ""
                response_text += delta
                yield postprocess(response_text)
        except Exception as e:
            error_message = f"Error: {str(e)}"
            return error_message

    return fn