File size: 3,083 Bytes
bee5263
 
 
3fbd422
 
 
 
bee5263
dbcfd8e
 
 
bee5263
dbcfd8e
 
 
 
 
 
 
3fbd422
dbcfd8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee5263
dbcfd8e
 
 
 
bee5263
 
 
 
 
dbcfd8e
 
 
 
 
 
bee5263
3fbd422
 
 
 
 
 
 
 
bee5263
 
 
 
 
 
 
 
 
 
 
 
 
ee2886a
bee5263
 
3fbd422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee5263
3fbd422
 
 
bee5263
 
 
fff1df0
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
import prompt_style

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# **************************************************
# import transformers
# import torch

# pipeline = transformers.pipeline(
#     "text-generation",
#     model=model_id,
#     model_kwargs={"torch_dtype": torch.bfloat16},
#     device_map="auto",
# )

def generate_1(item: Item):
    messages = [
        {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
        {"role": "user", "content": "Who are you?"},
    ]
    
    terminators = [
        pipeline.tokenizer.eos_token_id,
        pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
    ]
    
    outputs = pipeline(
        messages,
        max_new_tokens=item.max_new_tokens,
        eos_token_id=terminators,
        do_sample=True,
        temperature=item.temperature,
        top_p=item.top_p,
    )
    return outputs[0]["generated_text"][-1]

# **************************************************




client = InferenceClient(model_id)

class Item(BaseModel):
    prompt: str
    history: list
    system_prompt: str
    temperature: float = 0.6
    max_new_tokens: int = 1024
    top_p: float = 0.95
    seed : int = 42
    
app = FastAPI()

def format_prompt(item: Item):
    messages = [
        {"role": "system", "content": prompt_style.data},
    ]
    for it in item.history:
        messages.append[{"role" : "user", "content": it[0]}]
        messages.append[{"role" : "assistant", "content": it[1]}]
    return messages

def generate(item: Item):
    temperature = float(item.temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(item.top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=item.max_new_tokens,
        top_p=top_p,
        repetition_penalty=item.repetition_penalty,
        do_sample=True,
        seed=item.seed,
    )

    formatted_prompt = format_prompt(item)

    model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto",)

    input_ids = tokenizer.apply_chat_template(formatted_prompt, add_generation_prompt=True, return_tensors="pt").to(model.device)
    
    terminators = [
        tokenizer.eos_token_id,
        tokenizer.convert_tokens_to_ids("<|eot_id|>")
    ]

    outputs = model.generate(input_ids, eos_token_id=terminators, do_sample=True, **generate_kwargs)
    response = outputs[0][input_ids.shape[-1]:]
    return tokenizer.decode(response, skip_special_tokens=True)
    
    # stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
    # output = ""

    # for response in stream:
    #     output += response.token.text
    # return output

@app.post("/generate/")
async def generate_text(item: Item):
    ans = generate(item)
    return {"response": ans}