-
Notifications
You must be signed in to change notification settings - Fork 22
/
llama4openai-api.py
145 lines (122 loc) · 4.02 KB
/
llama4openai-api.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Adapted from https://gist.github.com/kinoc/8a042d8c5683725aa8c372274c02ea2f
import time
import torch
from flask import Flask, request
from flask.json import jsonify
from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer
app = Flask(__name__)
# ===== ARGUMENTS =====
model_name_or_path = "llama-wechat/"
device = "cuda"
# set up the llama model
tokenizer = LlamaTokenizer.from_pretrained(model_name_or_path)
model = LlamaForCausalLM.from_pretrained(
model_name_or_path,
load_in_8bit=False,
torch_dtype=torch.float16,
device_map="auto",
).eval()
def generate_prompt_llama(instruction):
return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:
"""
def evaluate_llama(
instruction,
temperature=0.1,
top_p=0.75,
top_k=40,
num_beams=1,
max_new_tokens=128,
**kwargs,
):
prompt = generate_prompt_llama(instruction)
print(f"prompt: {prompt}")
print(f"temperature: {temperature}")
print(f"top_p: {top_p}")
print(f"top_k: {top_k}")
print(f"num_beams: {num_beams}")
print(f"max_new_tokens: {max_new_tokens}")
inputs = tokenizer(prompt, return_tensors="pt")
input_ids = inputs["input_ids"].to(device)
generation_config = GenerationConfig(
temperature=temperature,
top_p=top_p,
top_k=top_k,
num_beams=num_beams,
**kwargs,
)
with torch.no_grad():
generation_output = model.generate(
input_ids=input_ids,
generation_config=generation_config,
return_dict_in_generate=True,
output_scores=True,
max_new_tokens=max_new_tokens,
)
s = generation_output.sequences[0]
output = tokenizer.decode(s, skip_special_tokens=True)
print(f"output: {output}")
gen_text = output.split("### Response:")[1].strip()
print(f"gen_text: {gen_text}")
return gen_text
def decode_kwargs(data):
kwargs = {}
if "n" in data:
kwargs["num_return_sequences"] = data["n"]
if "stop" in data:
kwargs["early_stopping"] = True
kwargs["stop_token"] = data["stop"]
if "suffix" in data:
kwargs["suffix"] = data["suffix"]
if "presence_penalty" in data:
kwargs["presence_penalty"] = data["presence_penalty"]
if "frequency_penalty" in data:
kwargs["repetition_penalty"] = data["frequency_penalty"]
if "repetition_penalty " in data:
kwargs["repetition_penalty"] = data["repetition_penalty "]
if "best_of " in data:
kwargs["num_return_sequences"] = data["best_of "]
return kwargs
@app.route("/chat/completions", methods=["POST"])
def chat_completions():
data = request.get_json(force=True)
model_name = data["model"]
messages = data["messages"]
prompt = messages[-1]["content"]
max_tokens = data.get("max_tokens", 16)
temperature = data.get("temperature", 1.0)
top_p = data.get("top_p", 0.75)
top_k = data.get("top_k", 40)
num_beams = data.get("num_beams", 1)
max_new_tokens = data.get("max_new_tokens", 256)
kwargs = decode_kwargs(data)
generated_text = evaluate_llama(
prompt,
temperature=temperature,
top_p=top_p,
top_k=top_k,
num_beams=num_beams,
max_new_tokens=max_new_tokens,
**kwargs,
)
prompt_tokens = len(tokenizer.encode(prompt))
completion_tokens = len(tokenizer.encode(generated_text))
total_tokens = prompt_tokens + completion_tokens
return jsonify(
{
"object": "chat.completion",
"id": "dummy",
"created": int(time.time()),
"model": model_name,
"choices": [{"message": {"role": "assistant", "content": generated_text}, "finish_reason": "stop"}],
"usage": {
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": total_tokens,
},
}
)
if __name__ == "__main__":
app.run()