feat: upgrade openai 1.x

This commit is contained in:
莘权 马 2023-12-18 11:31:08 +08:00
parent a9479843f6
commit 41361915a1
11 changed files with 35 additions and 26 deletions

View file

@ -66,7 +66,7 @@ async def text_to_speech(
return f"[{text}]({url})"
return audio_declaration + base64_data if base64_data else base64_data
raise openai.error.InvalidRequestError(
raise openai.InvalidRequestError(
message="AZURE_TTS_SUBSCRIPTION_KEY, AZURE_TTS_REGION, IFLYTEK_APP_ID, IFLYTEK_API_KEY, IFLYTEK_API_SECRET error",
param={},
)

View file

@ -171,7 +171,7 @@ class BrainMemory(pydantic.BaseModel):
if summary:
await self.set_history_summary(history_summary=summary, redis_key=CONFIG.REDIS_KEY, redis_conf=CONFIG.REDIS)
return summary
raise openai.error.InvalidRequestError(message="text too long", param=None)
raise openai.InvalidRequestError(message="text too long", param=None)
async def _metagpt_summarize(self, max_words=200, **kwargs):
if not self.history:

View file

@ -19,6 +19,8 @@ class FireWorksGPTAPI(OpenAIGPTAPI):
RateLimiter.__init__(self, rpm=self.rpm)
def __init_fireworks(self, config: "Config"):
openai.api_key = config.fireworks_api_key
openai.api_base = config.fireworks_api_base
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you
# instantiate the client, e.g. 'OpenAI(api_base=config.fireworks_api_base)'
# openai.api_key = config.fireworks_api_key
# openai.api_base = config.fireworks_api_base
self.rpm = int(config.get("RPM", 10))

View file

@ -2,8 +2,6 @@
# -*- coding: utf-8 -*-
# @Desc : self-host open llm model with openai-compatible interface
import openai
from metagpt.config import CONFIG
from metagpt.logs import logger
from metagpt.provider.openai_api import OpenAIGPTAPI, RateLimiter
@ -35,13 +33,14 @@ class OpenLLMCostManager(CostManager):
class OpenLLMGPTAPI(OpenAIGPTAPI):
def __init__(self):
self.__init_openllm(CONFIG)
self.llm = openai
self.model = CONFIG.open_llm_api_model
self.auto_max_tokens = False
self._cost_manager = OpenLLMCostManager()
RateLimiter.__init__(self, rpm=self.rpm)
def __init_openllm(self, config: "Config"):
openai.api_key = "sk-xx" # self-host api doesn't need api-key, use the default value
openai.api_base = config.open_llm_api_base
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you
# instantiate the client, e.g. 'OpenAI(api_base=config.open_llm_api_base)'
# openai.api_key = "sk-xx" # self-host api doesn't need api-key, use the default value
# openai.api_base = config.open_llm_api_base
self.rpm = int(config.get("RPM", 10))

View file

@ -5,7 +5,6 @@
import json
from enum import Enum
import openai
import zhipuai
from requests import ConnectionError
from tenacity import (
@ -48,7 +47,8 @@ class ZhiPuAIGPTAPI(BaseGPTAPI):
def __init_zhipuai(self, config: CONFIG):
assert config.zhipuai_api_key
zhipuai.api_key = config.zhipuai_api_key
openai.api_key = zhipuai.api_key # due to use openai sdk, set the api_key but it will't be used.
# due to use openai sdk, set the api_key but it will't be used.
# openai.api_key = zhipuai.api_key # due to use openai sdk, set the api_key but it will't be used.
def _const_kwargs(self, messages: list[dict]) -> dict:
kwargs = {"model": self.model, "prompt": messages, "temperature": 0.3}

View file

@ -10,8 +10,8 @@ import asyncio
import base64
import aiohttp
import openai
import requests
from openai import AsyncOpenAI
from metagpt.config import CONFIG, Config
from metagpt.logs import logger
@ -23,6 +23,11 @@ class OpenAIText2Image:
:param openai_api_key: OpenAI API key, For more details, checkout: `https://platform.openai.com/account/api-keys`
"""
self.openai_api_key = openai_api_key if openai_api_key else CONFIG.OPENAI_API_KEY
self._client = AsyncOpenAI(api_key=self.openai_api_key, base_url=CONFIG.openai_api_base)
def __del__(self):
if self._client:
self._client.close()
async def text_2_image(self, text, size_type="1024x1024"):
"""Text to image
@ -32,16 +37,7 @@ class OpenAIText2Image:
:return: The image data is returned in Base64 encoding.
"""
try:
result = await openai.Image.acreate(
api_key=CONFIG.OPENAI_API_KEY,
api_base=CONFIG.OPENAI_API_BASE,
api_type=None,
api_version=None,
organization=None,
prompt=text,
n=1,
size=size_type,
)
result = await self._client.images.generate(prompt=text, n=1, size=size_type)
except Exception as e:
logger.error(f"An error occurred:{e}")
return ""