Skip to content

Commit 59715c0

Browse files
authored
Merge pull request #1561 from zhaoweiguo/patch-3
fix bug in multi llm
2 parents ad30d2f + 4255acc commit 59715c0

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

metagpt/roles/role.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _check_actions(self):
247247
return self
248248

249249
def _init_action(self, action: Action):
250-
if not action.private_config:
250+
if not action.private_llm:
251251
action.set_llm(self.llm, override=True)
252252
else:
253253
action.set_llm(self.llm, override=False)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
llm:
2+
api_type: "openai" # or azure / ollama / groq etc.
3+
base_url: "YOUR_gpt-3.5-turbo_BASE_URL"
4+
api_key: "YOUR_gpt-3.5-turbo_API_KEY"
5+
model: "gpt-3.5-turbo" # or gpt-3.5-turbo
6+
# proxy: "YOUR_gpt-3.5-turbo_PROXY" # for LLM API requests
7+
# timeout: 600 # Optional. If set to 0, default value is 300.
8+
# Details: https://azure.microsoft.com/en-us/pricing/details/cognitive-services/openai-service/
9+
pricing_plan: "" # Optional. Use for Azure LLM when its model name is not the same as OpenAI's
10+
11+
models:
12+
"YOUR_MODEL_NAME_1": # model: "gpt-4-turbo" # or gpt-3.5-turbo
13+
api_type: "openai" # or azure / ollama / groq etc.
14+
base_url: "YOUR_MODEL_1_BASE_URL"
15+
api_key: "YOUR_MODEL_1_API_KEY"
16+
# proxy: "YOUR_MODEL_1_PROXY" # for LLM API requests
17+
# timeout: 600 # Optional. If set to 0, default value is 300.
18+
# Details: https://azure.microsoft.com/en-us/pricing/details/cognitive-services/openai-service/
19+
pricing_plan: "" # Optional. Use for Azure LLM when its model name is not the same as OpenAI's
20+
"YOUR_MODEL_NAME_2": # model: "gpt-4-turbo" # or gpt-3.5-turbo
21+
api_type: "openai" # or azure / ollama / groq etc.
22+
base_url: "YOUR_MODEL_2_BASE_URL"
23+
api_key: "YOUR_MODEL_2_API_KEY"
24+
proxy: "YOUR_MODEL_2_PROXY" # for LLM API requests
25+
# timeout: 600 # Optional. If set to 0, default value is 300.
26+
# Details: https://azure.microsoft.com/en-us/pricing/details/cognitive-services/openai-service/
27+
pricing_plan: "" # Optional. Use for Azure LLM when its model name is not the same as OpenAI's
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from metagpt.actions.action import Action
2+
from metagpt.config2 import Config
3+
from metagpt.const import TEST_DATA_PATH
4+
from metagpt.context import Context
5+
from metagpt.provider.llm_provider_registry import create_llm_instance
6+
from metagpt.roles.role import Role
7+
8+
9+
def test_set_llm():
10+
config1 = Config.default()
11+
config2 = Config.default()
12+
config2.llm.model = "gpt-3.5-turbo"
13+
14+
context = Context(config=config1)
15+
act = Action(context=context)
16+
assert act.config.llm.model == config1.llm.model
17+
18+
llm2 = create_llm_instance(config2.llm)
19+
act.llm = llm2
20+
assert act.llm.model == llm2.model
21+
22+
role = Role(context=context)
23+
role.set_actions([act])
24+
assert act.llm.model == llm2.model
25+
26+
role1 = Role(context=context)
27+
act1 = Action(context=context)
28+
assert act1.config.llm.model == config1.llm.model
29+
act1.config = config2
30+
role1.set_actions([act1])
31+
assert act1.llm.model == llm2.model
32+
33+
# multiple LLM
34+
35+
config3_path = TEST_DATA_PATH / "config/config2_multi_llm.yaml"
36+
dict3 = Config.read_yaml(config3_path)
37+
config3 = Config(**dict3)
38+
context3 = Context(config=config3)
39+
role3 = Role(context=context3)
40+
act3 = Action(context=context3, llm_name_or_type="YOUR_MODEL_NAME_1")
41+
assert act3.config.llm.model == "gpt-3.5-turbo"
42+
assert act3.llm.model == "gpt-4-turbo"
43+
role3.set_actions([act3])
44+
assert act3.config.llm.model == "gpt-3.5-turbo"
45+
assert act3.llm.model == "gpt-4-turbo"

0 commit comments

Comments
 (0)