File size: 964 Bytes
d857963
 
 
eb8ac8a
d857963
 
 
eb8ac8a
d857963
eb8ac8a
 
 
 
 
 
 
 
 
d857963
eb8ac8a
 
 
d857963
 
 
 
2e5f69a
d857963
 
 
eb8ac8a
 
 
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
from fastapi import FastAPI
import requests
from llama_cpp import Llama 
import threading

app = FastAPI()

llm = None

def start_llm():
    global llm  # Adicione esta linha para modificar a variável global
    llm = Llama(model_path="./tinyllama-1.1b-chat.gguf")
    
@app.post("/health")
def health_check():
    return {"status": "ok"}

@app.post("/deployllm")
async def stream(item: dict):
    
    if llm is None:
        raise ValueError("modelo carregando, por favor tente mais tarde")
	
    if 'prompt' not in item.keys():
        raise ValueError("prompt é obrigatório")

    prompt = "<|system|>You are a helpfull assistant</s><|user|>"+item['prompt']+"</s><|assistant|>"
    temperatura = item['temperatura'] if 'temperatura' in item.keys() else 0.2
    max_tokens = item['max_tokens'] if 'max_tokens' in item.keys() else 512
    
    return llm(prompt, max_tokens=max_tokens, temperature=temperatura)
    
threading.Thread(target=start_llm).start()