Skip to content

Commit cc774db

Browse files
committed
simplify code
1 parent 119385c commit cc774db

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

examples/hello_world.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
async def ask_and_print(question: str, llm: LLM, system_prompt) -> str:
1515
logger.info(f"Q: {question}")
16-
rsp = await llm.aask(question, system_msgs=[system_prompt])
16+
rsp = await llm.aask(question, system_msgs=[system_prompt], stream=True)
17+
if llm.reasoning_content:
18+
logger.info(f"A reasoning: {llm.reasoning_content}")
1719
logger.info(f"A: {rsp}")
1820
return rsp
1921

metagpt/provider/openai_api.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _make_client_kwargs(self) -> dict:
7979
def _get_proxy_params(self) -> dict:
8080
params = {}
8181
if self.config.proxy:
82-
params = {"proxies": self.config.proxy}
82+
params = {"proxy": self.config.proxy}
8383
if self.config.base_url:
8484
params["base_url"] = self.config.base_url
8585

@@ -94,13 +94,16 @@ async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFI
9494
collected_reasoning_messages = []
9595
has_finished = False
9696
async for chunk in response:
97-
if hasattr(chunk.choices[0].delta, "reasoning_content"):
98-
collected_reasoning_messages.append(chunk.choices[0].delta.reasoning_content) # for deepseek
97+
if not chunk.choices:
9998
continue
100-
chunk_message = chunk.choices[0].delta.content or "" if chunk.choices else "" # extract the message
101-
finish_reason = (
102-
chunk.choices[0].finish_reason if chunk.choices and hasattr(chunk.choices[0], "finish_reason") else None
103-
)
99+
100+
choice0 = chunk.choices[0]
101+
choice_delta = choice0.delta
102+
if hasattr(choice_delta, "reasoning_content") and choice_delta.reasoning_content:
103+
collected_reasoning_messages.append(choice_delta.reasoning_content) # for deepseek
104+
continue
105+
chunk_message = choice_delta.content or "" # extract the message
106+
finish_reason = choice0.finish_reason if hasattr(choice0, "finish_reason") else None
104107
log_llm_stream(chunk_message)
105108
collected_messages.append(chunk_message)
106109
chunk_has_usage = hasattr(chunk, "usage") and chunk.usage
@@ -111,13 +114,10 @@ async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFI
111114
if finish_reason:
112115
if chunk_has_usage:
113116
# Some services have usage as an attribute of the chunk, such as Fireworks
114-
if isinstance(chunk.usage, CompletionUsage):
115-
usage = chunk.usage
116-
else:
117-
usage = CompletionUsage(**chunk.usage)
118-
elif hasattr(chunk.choices[0], "usage"):
117+
usage = CompletionUsage(**chunk.usage) if isinstance(chunk.usage, dict) else chunk.usage
118+
elif hasattr(choice0, "usage"):
119119
# The usage of some services is an attribute of chunk.choices[0], such as Moonshot
120-
usage = CompletionUsage(**chunk.choices[0].usage)
120+
usage = CompletionUsage(**choice0.usage)
121121
has_finished = True
122122

123123
log_llm_stream("\n")

metagpt/utils/cost_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ def update_cost(self, prompt_tokens: int, completion_tokens: int, model: str):
144144
cost = (prompt_tokens * token_costs["prompt"] + completion_tokens * token_costs["completion"]) / 1000000
145145
self.total_cost += cost
146146
logger.info(
147-
f"Total running cost: ${self.total_cost:.4f}"
147+
f"Total running cost: ${self.total_cost:.4f}, "
148148
f"Current cost: ${cost:.4f}, prompt_tokens: {prompt_tokens}, completion_tokens: {completion_tokens}"
149149
)

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ lancedb==0.4.0
1313
loguru==0.6.0
1414
meilisearch==0.21.0
1515
numpy~=1.26.4
16-
openai~=1.39.0
16+
openai~=1.64.0
1717
openpyxl~=3.1.5
1818
beautifulsoup4==4.12.3
1919
pandas==2.1.1
@@ -59,7 +59,7 @@ nbformat==5.9.2
5959
ipython==8.17.2
6060
ipykernel==6.27.1
6161
scikit_learn==1.3.2
62-
typing-extensions==4.9.0
62+
typing-extensions==4.11.0
6363
socksio~=1.0.0
6464
gitignore-parser==0.1.9
6565
# connexion[uvicorn]~=3.0.5 # Used by metagpt/tools/openapi_v3_hello.py

0 commit comments

Comments
 (0)