add: 添加chatgpt chat api逻辑

This commit is contained in:
taylor
2024-10-14 00:16:19 +08:00
parent 8c16faa34a
commit 0f7a2c4b33
4 changed files with 59 additions and 16 deletions

View File

@@ -1,21 +1,62 @@
import json
from aiohttp import ClientSession
from maubot import Plugin
from mautrix.types import MessageEvent
from mautrix.util.config import BaseProxyConfig
import maubot_llmplus.platforms
from maubot_llmplus.platforms import Platform, ChatCompletion
from maubot_llmplus.plugin import AbsExtraConfigPlugin
class OpenAi(Platform):
max_tokens: int
temperature: int
def __init__(self, config: BaseProxyConfig, http: ClientSession) -> None:
super().__init__(config, http)
self.max_tokens = self.config['max_tokens']
self.temperature = self.config['temperature']
async def create_chat_completion(self, plugin: Plugin, evt: MessageEvent) -> ChatCompletion:
# 获取系统提示词
# 获取额外的其他角色的提示词: role: user role: system
async def create_chat_completion(self, plugin: AbsExtraConfigPlugin, evt: MessageEvent) -> ChatCompletion:
full_context = []
context = await maubot_llmplus.platforms.get_context(plugin, self, evt)
full_context.extend(list(context))
pass
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.config['gpt_api_key']}"
}
data = {
"model": self.model,
"messages": full_context,
}
if 'max_tokens' in self.config and self.max_tokens:
data["max_tokens"] = self.max_tokens
if 'temperature' in self.config and self.temperature:
data["temperature"] = self.temperature
endpoint = f"{self.url}/v1/chat/completions"
async with self.http.post(
endpoint, headers=headers, data=json.dumps(data)
) as response:
# plugin.log.debug(f"响应内容:{response.status}, {await response.json()}")
if response.status != 200:
return ChatCompletion(
message={},
finish_reason=f"Error: {await response.text()}",
model=None
)
response_json = await response.json()
choice = response_json["choices"][0]
return ChatCompletion(
message=choice["message"],
finish_reason=choice["finish_reason"],
model=choice.get("model", None)
)
def get_type(self) -> str:
return "openai"
@@ -26,7 +67,7 @@ class Anthropic(Platform):
def __init__(self, config: BaseProxyConfig, http: ClientSession) -> None:
super().__init__(config, http)
async def create_chat_completion(self, plugin: Plugin, evt: MessageEvent) -> ChatCompletion:
async def create_chat_completion(self, plugin: AbsExtraConfigPlugin, evt: MessageEvent) -> ChatCompletion:
# 获取系统提示词
# 获取额外的其他角色的提示词: role: user role: system