Update app.py
Browse files
app.py
CHANGED
|
@@ -6,25 +6,41 @@ app = FastAPI()
|
|
| 6 |
|
| 7 |
pipe = pipeline(
|
| 8 |
"text-generation",
|
| 9 |
-
model="
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
class Req(BaseModel):
|
| 13 |
message: str
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@app.post("/chat")
|
| 16 |
def chat(req: Req):
|
| 17 |
-
prompt =
|
| 18 |
|
| 19 |
out = pipe(
|
| 20 |
prompt,
|
| 21 |
-
max_new_tokens=
|
| 22 |
-
temperature=0.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
text = out[0]["generated_text"]
|
| 26 |
|
| 27 |
-
|
| 28 |
-
reply = text.split("[/INST]")[-1].strip()
|
| 29 |
|
| 30 |
return {"reply": reply}
|
|
|
|
| 6 |
|
| 7 |
pipe = pipeline(
|
| 8 |
"text-generation",
|
| 9 |
+
model="microsoft/phi-3-mini-4k-instruct",
|
| 10 |
+
device_map="auto"
|
| 11 |
)
|
| 12 |
|
| 13 |
class Req(BaseModel):
|
| 14 |
message: str
|
| 15 |
|
| 16 |
+
def build_prompt(user_msg: str) -> str:
|
| 17 |
+
return (
|
| 18 |
+
"<|system|>\n"
|
| 19 |
+
"You are a helpful, clear, and concise assistant. "
|
| 20 |
+
"Give accurate and well-structured answers. Avoid unnecessary fluff."
|
| 21 |
+
"<|end|>\n"
|
| 22 |
+
"<|user|>\n"
|
| 23 |
+
f"{user_msg}"
|
| 24 |
+
"<|end|>\n"
|
| 25 |
+
"<|assistant|>\n"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
@app.post("/chat")
|
| 29 |
def chat(req: Req):
|
| 30 |
+
prompt = build_prompt(req.message)
|
| 31 |
|
| 32 |
out = pipe(
|
| 33 |
prompt,
|
| 34 |
+
max_new_tokens=200,
|
| 35 |
+
temperature=0.6,
|
| 36 |
+
top_p=0.9,
|
| 37 |
+
do_sample=True,
|
| 38 |
+
repetition_penalty=1.1,
|
| 39 |
+
eos_token_id=pipe.tokenizer.eos_token_id
|
| 40 |
)
|
| 41 |
|
| 42 |
text = out[0]["generated_text"]
|
| 43 |
|
| 44 |
+
reply = text.split("<|assistant|>")[-1].strip()
|
|
|
|
| 45 |
|
| 46 |
return {"reply": reply}
|