File size: 1,691 Bytes
4e21e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from vllm import LLM
from vllm.sampling_params import SamplingParams
import mergekit
from fastapi import FastAPI
from uvicorn import run

app = FastAPI()

model_name = "Hjgugugjhuhjggg/mergekit-ties-tzamfyy"
mergekit_model = mergekit.MergeKitModel(model_name)

SYSTEM_PROMPT = "¡Hola! Soy un modelo de lenguaje avanzado. Estoy aquí para ayudarte con cualquier pregunta o tema que desees discutir. ¿En qué puedo ayudarte hoy?"


def divide_texto(texto, max_tokens=512):
    mensajes = []
    texto_actual = ""
    tokens_actuales = 0
    for palabra in texto.split():
        if tokens_actuales + len(palabra) > max_tokens:
            mensajes.append(texto_actual.strip())
            texto_actual = palabra + " "
            tokens_actuales = len(palabra) + 1
        else:
            texto_actual += palabra + " "
            tokens_actuales += len(palabra) + 1
    if texto_actual.strip():
        mensajes.append(texto_actual.strip())
    return mensajes

@app.post("/chat")
async def chat(texto: str):
    mensajes = divide_texto(texto)

    # Nuevo mensaje
    mensaje = {"role": "user", "content": []}

    for mensaje_texto in mensajes:
        mensaje["content"].append({"type": "text", "text": mensaje_texto})

    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        mensaje,
    ]

    sampling_params = SamplingParams(max_tokens=512)

    llm = LLM(model=mergekit_model, config_format="llama", load_format="llama", tokenizer_mode="llama", tensor_parallel_size=8)

    outputs = llm.chat(messages, sampling_params=sampling_params)

    return {"response": outputs[0].outputs[0].text}

if __name__ == "__main__":
    run(app, host="0.0.0.0", port=8000)