airs chat completion
Browse files- README.md +3 -0
- app.py +7 -16
- gemini_api.py +42 -110
- openai_model.py +14 -23
README.md
CHANGED
|
@@ -8,3 +8,6 @@ pinned: false
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
此项目实现在huggingface运行的映射访问到各个大模型。各个大模型安装各自的api规范进行调用就可以了。
|
app.py
CHANGED
|
@@ -1,13 +1,9 @@
|
|
| 1 |
# 在终端运行命令:uvicorn app:app --host 0.0.0.0 --port 7860 --reload
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List
|
| 5 |
-
import logging
|
| 6 |
|
| 7 |
-
|
| 8 |
-
# import json, time, uuid, os
|
| 9 |
-
|
| 10 |
-
from openai_model import TextCompletionRequest, TextCompletionResponse, ChatCompletionRequest, TbsChatCompletionRequest
|
| 11 |
from models import *
|
| 12 |
|
| 13 |
app = FastAPI()
|
|
@@ -16,11 +12,6 @@ app = FastAPI()
|
|
| 16 |
def greet_json():
|
| 17 |
return {"Hello": "World!"}
|
| 18 |
|
| 19 |
-
# 请求和响应模型
|
| 20 |
-
# class ChatRequest(BaseModel):
|
| 21 |
-
# model: str
|
| 22 |
-
# messages: List[dict]
|
| 23 |
-
|
| 24 |
class ChatResponse(BaseModel):
|
| 25 |
model: str
|
| 26 |
messages: List[dict]
|
|
@@ -31,9 +22,9 @@ async def create_text_completion(request: TextCompletionRequest):
|
|
| 31 |
response = ModelClass().create_text_completion(request)
|
| 32 |
return response
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
ModelClass = class_by_modelname(request.model)
|
| 37 |
-
response = ModelClass().
|
| 38 |
-
return response
|
| 39 |
-
|
|
|
|
| 1 |
# 在终端运行命令:uvicorn app:app --host 0.0.0.0 --port 7860 --reload
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List
|
|
|
|
| 5 |
|
| 6 |
+
from openai_model import TextCompletionRequest, AirsChatCompletionRequest
|
|
|
|
|
|
|
|
|
|
| 7 |
from models import *
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 12 |
def greet_json():
|
| 13 |
return {"Hello": "World!"}
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
class ChatResponse(BaseModel):
|
| 16 |
model: str
|
| 17 |
messages: List[dict]
|
|
|
|
| 22 |
response = ModelClass().create_text_completion(request)
|
| 23 |
return response
|
| 24 |
|
| 25 |
+
# 下面是 airs 的 chat completion 接口
|
| 26 |
+
@app.post("/airs/v1/chat/completions")
|
| 27 |
+
async def airs_create_chat_response(request: AirsChatCompletionRequest):
|
| 28 |
ModelClass = class_by_modelname(request.model)
|
| 29 |
+
response = ModelClass().do_request(request)
|
| 30 |
+
return response
|
|
|
gemini_api.py
CHANGED
|
@@ -1,12 +1,50 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import uuid, time
|
| 4 |
-
# from google.generativeai import BaseGenerateContentResponse
|
| 5 |
|
| 6 |
class gemini:
|
| 7 |
def __init__(self):
|
| 8 |
pass
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def create_text_completion(self, request: TextCompletionRequest):
|
| 12 |
model = genai.GenerativeModel(request.model)
|
|
@@ -32,110 +70,4 @@ class gemini:
|
|
| 32 |
}
|
| 33 |
}
|
| 34 |
return response
|
| 35 |
-
|
| 36 |
-
def create_chat_response(self, request: TbsChatCompletionRequest):
|
| 37 |
-
print('request',request)
|
| 38 |
-
model=request.model
|
| 39 |
-
system=request.system
|
| 40 |
-
prompt=request.prompt
|
| 41 |
-
messages=request.messages
|
| 42 |
-
|
| 43 |
-
combined_history = []
|
| 44 |
-
if messages and (len(messages) > 0):
|
| 45 |
-
for message in messages:
|
| 46 |
-
# 检查 'role' 是否等于 'system',如果是,则将其更改为 'user'
|
| 47 |
-
role = 'user' if message['role'] == 'system' else message['role']
|
| 48 |
-
combined_history.append({
|
| 49 |
-
"role": role,
|
| 50 |
-
"parts": [message['content']]
|
| 51 |
-
})
|
| 52 |
-
if system:
|
| 53 |
-
combined_history.append({"role": "user", "parts": [system]})
|
| 54 |
-
print('combined_history',combined_history)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
model = genai.GenerativeModel(request.model)
|
| 58 |
-
chat = model.start_chat(history=combined_history)
|
| 59 |
-
gemini_response = chat.send_message(prompt)
|
| 60 |
-
# print(f"\n\ngemini_response\n{gemini_response}")
|
| 61 |
-
|
| 62 |
-
response = {
|
| 63 |
-
"id": f"cmpl-{uuid.uuid4()}",
|
| 64 |
-
"object": "chat.completion",
|
| 65 |
-
"created": int(time.time()),
|
| 66 |
-
"model": request.model,
|
| 67 |
-
"choices": [
|
| 68 |
-
{
|
| 69 |
-
"message": {
|
| 70 |
-
"role": "assistant", "content": f"\n\n{gemini_response.candidates[0].content.parts[0].text}"
|
| 71 |
-
},
|
| 72 |
-
"index": gemini_response.candidates[0].index,
|
| 73 |
-
"logprobs": None,
|
| 74 |
-
"finish_reason": gemini_response.candidates[0].finish_reason
|
| 75 |
-
}
|
| 76 |
-
],
|
| 77 |
-
"usage": {
|
| 78 |
-
"prompt_tokens": gemini_response.usage_metadata.prompt_token_count,
|
| 79 |
-
"completion_tokens": gemini_response.usage_metadata.candidates_token_count,
|
| 80 |
-
"total_tokens": gemini_response.usage_metadata.total_token_count
|
| 81 |
-
}
|
| 82 |
-
}
|
| 83 |
-
return response
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
"""
|
| 88 |
-
{
|
| 89 |
-
"model": 'gemini-1.5-flash',
|
| 90 |
-
"system": '你的角色是一个超级厉害的提示词工程师,当有人问你是谁时,你就说你是一个超级厉害的提示词工程师就可以。',
|
| 91 |
-
"prompt": '请介绍你自己',
|
| 92 |
-
"messages": [
|
| 93 |
-
{
|
| 94 |
-
'role': 'user',
|
| 95 |
-
'content': '你是一个专业的提示词专家,如果有人问你,你是谁的时候,你就说你是专业的提示词专家'
|
| 96 |
-
},
|
| 97 |
-
{
|
| 98 |
-
'role': 'user',
|
| 99 |
-
'content': '你是谁?'
|
| 100 |
-
},
|
| 101 |
-
{'role': 'model',
|
| 102 |
-
'content': '我是一个专业的提示词专家'
|
| 103 |
-
},
|
| 104 |
-
{
|
| 105 |
-
'role': 'user',
|
| 106 |
-
'content': '巴拉巴拉'
|
| 107 |
-
}
|
| 108 |
-
]
|
| 109 |
-
}
|
| 110 |
-
combined_history = [
|
| 111 |
-
{
|
| 112 |
-
'role': 'user',
|
| 113 |
-
'parts': [
|
| 114 |
-
'你的角色是一个超级厉害的提示词工程师,当有人问你是谁时,你就说你是一个超级厉害的提示词工程师就可以。'
|
| 115 |
-
]
|
| 116 |
-
},
|
| 117 |
-
{'role': 'user',
|
| 118 |
-
'parts': [
|
| 119 |
-
'你是一个专业的提示词专家,如果有人问你,你是谁的时候,你就说你是专业的提示词专家'
|
| 120 |
-
]
|
| 121 |
-
},
|
| 122 |
-
{
|
| 123 |
-
'role': 'user',
|
| 124 |
-
'parts': [
|
| 125 |
-
'你是谁?'
|
| 126 |
-
]
|
| 127 |
-
},
|
| 128 |
-
{
|
| 129 |
-
'role': 'model',
|
| 130 |
-
'parts': [
|
| 131 |
-
'我是一个专业的提示词专家'
|
| 132 |
-
]
|
| 133 |
-
},
|
| 134 |
-
{
|
| 135 |
-
'role': 'user',
|
| 136 |
-
'parts': [
|
| 137 |
-
'巴拉巴拉'
|
| 138 |
-
]
|
| 139 |
-
}
|
| 140 |
-
]
|
| 141 |
-
"""
|
|
|
|
| 1 |
+
# gemini 的 api 文档地址:
|
| 2 |
+
|
| 3 |
+
from openai_model import TextCompletionRequest
|
| 4 |
import google.generativeai as genai
|
| 5 |
import uuid, time
|
|
|
|
| 6 |
|
| 7 |
class gemini:
|
| 8 |
def __init__(self):
|
| 9 |
pass
|
| 10 |
+
|
| 11 |
+
def do_request(self, request):
|
| 12 |
+
model = genai.GenerativeModel(request.model)
|
| 13 |
+
messages=request.messages
|
| 14 |
+
if not messages:
|
| 15 |
+
return "No messages provided"
|
| 16 |
+
if messages[0]["role"] == 'system':
|
| 17 |
+
messages[0]["role"]='user'
|
| 18 |
+
content=messages[0]["content"]
|
| 19 |
+
messages[0]["content"] = f"系统消息:{content}"
|
| 20 |
+
message = messages[-1]
|
| 21 |
+
history=messages
|
| 22 |
+
history.pop()
|
| 23 |
+
converted_history = [{'role': msg['role'], 'parts': msg['content']} for msg in messages ]
|
| 24 |
+
|
| 25 |
+
chat = model.start_chat(history=converted_history)
|
| 26 |
+
gemini_response = chat.send_message(message["content"])
|
| 27 |
+
response = {
|
| 28 |
+
"candidates":[
|
| 29 |
+
{
|
| 30 |
+
"content": {
|
| 31 |
+
"parts": [
|
| 32 |
+
{
|
| 33 |
+
"text": gemini_response.candidates[0].content.parts[0].text
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
"usage_metadata": {
|
| 40 |
+
"promptTokenCount": gemini_response.usage_metadata.prompt_token_count,
|
| 41 |
+
"candidatesTokenCount": gemini_response.usage_metadata.candidates_token_count,
|
| 42 |
+
"totalTokenCount": gemini_response.usage_metadata.total_token_count
|
| 43 |
+
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
return response
|
| 47 |
+
|
| 48 |
|
| 49 |
def create_text_completion(self, request: TextCompletionRequest):
|
| 50 |
model = genai.GenerativeModel(request.model)
|
|
|
|
| 70 |
}
|
| 71 |
}
|
| 72 |
return response
|
| 73 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
openai_model.py
CHANGED
|
@@ -1,34 +1,25 @@
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
from typing import List, Optional
|
| 3 |
|
|
|
|
|
|
|
| 4 |
# 请求和响应模型
|
| 5 |
class TextCompletionRequest(BaseModel):
|
| 6 |
model: str
|
| 7 |
prompt: str
|
| 8 |
|
| 9 |
-
class TextCompletionResponse(BaseModel):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
class ChatCompletionRequest(BaseModel):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
# ########## 【特殊设计注意】 ###########
|
| 22 |
-
#
|
| 23 |
-
# 下面的数据结构类里包含了system 字符串,此设计和 OpenAI 不一致,
|
| 24 |
-
# 当其为None 时,默认为空字符串,不改变 system 参数
|
| 25 |
-
# 当其为 '' ,时,会清空 system 参数
|
| 26 |
-
# 当其为正常字符串时,更新fastapi服务器上的system参数
|
| 27 |
-
# 为了保证多次设置系统提示词时最新的信息优先生效,system参数会放到历史纪录的最末尾一条,
|
| 28 |
-
# 而且存储时,system不存储到messages里,避免多次重复
|
| 29 |
-
class TbsChatCompletionRequest(BaseModel):
|
| 30 |
model: str
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# messages: List[dict] | None = None
|
| 34 |
-
messages: Optional[List[dict]] = None
|
|
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
from typing import List, Optional
|
| 3 |
|
| 4 |
+
# 注意:下面的代码,主要聚焦在ChatCompletion上,而不是TextCompletion上。
|
| 5 |
+
|
| 6 |
# 请求和响应模型
|
| 7 |
class TextCompletionRequest(BaseModel):
|
| 8 |
model: str
|
| 9 |
prompt: str
|
| 10 |
|
| 11 |
+
# class TextCompletionResponse(BaseModel):
|
| 12 |
+
# text: str
|
| 13 |
+
# model: str
|
| 14 |
+
# candidates: List = []
|
| 15 |
|
| 16 |
+
# class ChatCompletionRequest(BaseModel):
|
| 17 |
+
# model: str
|
| 18 |
+
# prompt: str
|
| 19 |
+
# # messages: List[dict] | None = None
|
| 20 |
+
# messages: Optional[List[dict]] = None
|
| 21 |
|
| 22 |
+
class AirsChatCompletionRequest(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
model: str
|
| 24 |
+
messages: List[dict]
|
| 25 |
+
|
|
|
|
|
|