xulh commited on
Commit
1e5f011
·
1 Parent(s): 433854d

代码初始化

Browse files
Files changed (1) hide show
  1. inference/inference.py +27 -0
inference/inference.py CHANGED
@@ -80,6 +80,33 @@ async def chat_completion(token: str = Body(...), messages: list = Body(...)):
80
  raise HTTPException(status_code=500, detail=f"Error generating chat completion: {str(e)}")
81
 
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  @router.post("/api-inference/")
84
  async def api_inference(
85
  authorization: str = Header(...),
 
80
  raise HTTPException(status_code=500, detail=f"Error generating chat completion: {str(e)}")
81
 
82
 
83
+ @router.post("/format-prompt/")
84
+ async def chat_completion(token: str = Body(...), messages: list = Body(...)):
85
+ try:
86
+ # 创建 InferenceClient
87
+ client = InferenceClient(api_key=token)
88
+ print("问题:", messages)
89
+ messages.append({
90
+ "role": "system",
91
+ "content": "You are a highly intelligent image generation text optimizer. Your role is to take the user's "
92
+ "input prompt and enhance it, ensuring it is precise, vivid, and detailed, while maintaining "
93
+ "its intended meaning. Always provide the enhanced version in English."
94
+ })
95
+
96
+ # 使用 chat API 请求生成模型的回答 备选模型 google/gemma-2-2b-it
97
+ completion = client.chat.completions.create(
98
+ model="mistralai/Mistral-Nemo-Instruct-2407",
99
+ messages=messages,
100
+ max_tokens=500
101
+ )
102
+
103
+ # 返回对话信息
104
+ return {"message": completion.choices[0].message}
105
+
106
+ except Exception as e:
107
+ raise HTTPException(status_code=500, detail=f"Error generating chat completion: {str(e)}")
108
+
109
+
110
  @router.post("/api-inference/")
111
  async def api_inference(
112
  authorization: str = Header(...),