Skip to content

Commit

Permalink
Merge branch 'geekan:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sablin39 authored Jul 9, 2023
2 parents e4a1b9f + 3882416 commit 559a5f4
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 133 deletions.
28 changes: 19 additions & 9 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@

# Do not modify here, create a new key.yaml, define OPENAI_API_KEY. The configuration of key.yaml has a higher priority and will not enter git
# DO NOT MODIFY THIS FILE, create a new key.yaml, define OPENAI_API_KEY.
# The configuration of key.yaml has a higher priority and will not enter git

#### if OpenAI

OPENAI_API_KEY: "YOUR_API_KEY"
#OPENAI_API_BASE: "YOUR_API_BASE"
OPENAI_API_MODEL: "gpt-4"
MAX_TOKENS: 1500
RPM: 10

#### if AZURE, check https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb

#OPENAI_API_TYPE: "azure"
#OPENAI_API_BASE: "YOUR_AZURE_ENDPOINT"
#OPENAI_API_KEY: "YOUR_AZURE_API_KEY"
#OPENAI_API_VERSION: "YOUR_AZURE_API_VERSION"
#DEPLOYMENT_ID: "YOUR_DEPLOYMENT_ID"

#### for Search

## Visit https://serpapi.com/ to get key.
#SERPAPI_API_KEY: "YOUR_API_KEY"
#
## Visit https://console.cloud.google.com/apis/credentials to get key.
#GOOGLE_API_KEY: "YOUR_API_KEY"
## Visit https://programmablesearchengine.google.com/controlpanel/create to get id.
#GOOGLE_CSE_ID: "YOUR_CSE_ID"
#
#AZURE_OPENAI_KEY: "YOUR_API_KEY"
#AZURE_OPENAI_ENDPOINT: "YOUR_API_BASE"
#AZURE_DEPLOYMENT_NAME: "gpt-35"
#AZURE_OPENAI_API_VERSION: "2023-03-15-preview"
#

#### for TTS

#AZURE_TTS_SUBSCRIPTION_KEY: "YOUR_API_KEY"
#AZURE_TTS_REGION: "eastus"
#AZURE_TTS_REGION: "eastus"
1 change: 1 addition & 0 deletions metagpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, yaml_file=default_yaml_file):
self.openai_api_rpm = self._get('RPM', 3)
self.openai_api_model = self._get('OPENAI_API_MODEL', "gpt-4")
self.max_tokens_rsp = self._get('MAX_TOKENS', 2048)
self.deployment_id = self._get('DEPLOYMENT_ID')

self.serpapi_api_key = self._get('SERPAPI_API_KEY')
self.google_api_key = self._get('GOOGLE_API_KEY')
Expand Down
2 changes: 1 addition & 1 deletion metagpt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_project_root():
while True:
if (current_path / '.git').exists() or \
(current_path / '.project_root').exists() or \
(current_path.name.lower() == 'metagpt'):
(current_path / '.gitignore').exists():
return current_path
parent_path = current_path.parent
if parent_path == current_path:
Expand Down
3 changes: 1 addition & 2 deletions metagpt/provider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
@File : __init__.py
"""

from metagpt.provider.openai_api import OpenAIGPTAPI
from metagpt.provider.azure_api import AzureGPTAPI
from metagpt.provider.openai_api import OpenAIGPTAPI
77 changes: 0 additions & 77 deletions metagpt/provider/azure_api.py

This file was deleted.

58 changes: 29 additions & 29 deletions metagpt/provider/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from metagpt.logs import logger

from metagpt.provider.base_gpt_api import BaseGPTAPI
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.utils.singleton import Singleton
from metagpt.utils.token_counter import count_message_tokens, TOKEN_COSTS, count_string_tokens

Expand Down Expand Up @@ -69,7 +69,6 @@ def __init__(self):
self.total_completion_tokens = 0
self.total_cost = 0
self.total_budget = 0
self.config = Config()

def update_cost(self, prompt_tokens, completion_tokens, model):
"""
Expand All @@ -87,9 +86,9 @@ def update_cost(self, prompt_tokens, completion_tokens, model):
+ completion_tokens * TOKEN_COSTS[model]["completion"]
) / 1000
self.total_cost += cost
logger.info(f"Total running cost: ${self.total_cost:.3f} | Max budget: ${self.config.max_budget:.3f} | "
logger.info(f"Total running cost: ${self.total_cost:.3f} | Max budget: ${CONFIG.max_budget:.3f} | "
f"Current cost: ${cost:.3f}, {prompt_tokens=}, {completion_tokens=}")
self.config.total_cost = self.total_cost
CONFIG.total_cost = self.total_cost

def get_total_prompt_tokens(self):
"""
Expand Down Expand Up @@ -128,10 +127,9 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter):
Check https://platform.openai.com/examples for examples
"""
def __init__(self):
self.config = Config()
self.__init_openai(self.config)
self.__init_openai(CONFIG)
self.llm = openai
self.model = self.config.openai_api_model
self.model = CONFIG.openai_api_model
self._cost_manager = CostManager()
RateLimiter.__init__(self, rpm=self.rpm)

Expand All @@ -146,12 +144,7 @@ def __init_openai(self, config):

async def _achat_completion_stream(self, messages: list[dict]) -> str:
response = await openai.ChatCompletion.acreate(
model=self.model,
messages=messages,
max_tokens=self.config.max_tokens_rsp,
n=1,
stop=None,
temperature=0,
**self._cons_kwargs(messages),
stream=True
)

Expand All @@ -172,27 +165,34 @@ async def _achat_completion_stream(self, messages: list[dict]) -> str:
self._update_costs(usage)
return full_reply_content

def _cons_kwargs(self, messages: list[dict]) -> dict:
if CONFIG.openai_api_type == 'azure':
kwargs = {
"deployment_id": CONFIG.deployment_id,
"messages": messages,
"max_tokens": CONFIG.max_tokens_rsp,
"n": 1,
"stop": None,
"temperature": 0.5
}
else:
kwargs = {
"model": self.model,
"messages": messages,
"max_tokens": CONFIG.max_tokens_rsp,
"n": 1,
"stop": None,
"temperature": 0.5
}
return kwargs

async def _achat_completion(self, messages: list[dict]) -> dict:
rsp = await self.llm.ChatCompletion.acreate(
model=self.model,
messages=messages,
max_tokens=self.config.max_tokens_rsp,
n=1,
stop=None,
temperature=0.5,
)
rsp = await self.llm.ChatCompletion.acreate(**self._cons_kwargs(messages))
self._update_costs(rsp.get('usage'))
return rsp

def _chat_completion(self, messages: list[dict]) -> dict:
rsp = self.llm.ChatCompletion.create(
model=self.model,
messages=messages,
max_tokens=self.config.max_tokens_rsp,
n=1,
stop=None,
temperature=0.5,
)
rsp = self.llm.ChatCompletion.create(**self._cons_kwargs(messages))
self._update_costs(rsp)
return rsp

Expand Down
15 changes: 0 additions & 15 deletions tests/metagpt/gpt_provider/test_azure_gpt_api.py

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit 559a5f4

Please sign in to comment.