|
from ctransformers import AutoModelForCausalLM |
|
from fastapi import FastAPI, Form |
|
from pydantic import BaseModel |
|
|
|
|
|
llm = AutoModelForCausalLM.from_pretrained("TheBloke/Llama-2-7B-Chat-GGUF", |
|
model_type='llama', |
|
max_new_tokens = 4096, |
|
threads = 3, |
|
) |
|
|
|
|
|
|
|
class validation(BaseModel): |
|
prompt: str |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.post("/llm_on_cpu") |
|
async def stream(item: validation): |
|
system_prompt = "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information." |
|
prompt = f''' |
|
[INST] <<SYS>> |
|
{system_prompt} |
|
<</SYS>> |
|
{item.prompt.strip()}[/INST] |
|
''' |
|
|
|
return llm(prompt) |
|
|