22
22
23
23
try :
24
24
from camel .agents import ChatAgent
25
- from camel .configs import ChatGPTConfig
26
25
from camel .messages import BaseMessage
27
26
from camel .models import ModelFactory
28
27
from camel .toolkits import OpenAIFunction
33
32
CAMEL_ENABLED = False
34
33
35
34
36
- def _find_model_platform_type (model_platform_name : str ) -> "ModelPlatformType" :
37
- for platform in ModelPlatformType :
38
- if platform . value . lower () == model_platform_name . lower ():
39
- return platform
40
- all_models = [platform .value for platform in ModelPlatformType ]
41
- raise ValueError (
42
- f"Model { model_platform_name } not found. Supported models are { all_models } "
43
- )
35
+ def _get_model_platform_type (model_platform_name : str ) -> "ModelPlatformType" :
36
+ try :
37
+ return ModelPlatformType ( model_platform_name )
38
+ except ValueError :
39
+ all_models = [platform .value for platform in ModelPlatformType ]
40
+ raise ValueError (
41
+ f"Model { model_platform_name } not found. Supported models are { all_models } "
42
+ )
44
43
45
44
46
- def _find_model_type (model_name : str ) -> "str | ModelType" :
47
- for model in ModelType :
48
- if model . value . lower () == model_name . lower ():
49
- return model
50
- return model_name
45
+ def _get_model_type (model_name : str ) -> "str | ModelType" :
46
+ try :
47
+ return ModelType ( model_name )
48
+ except ValueError :
49
+ return model_name
51
50
52
51
53
52
def _convert_action_to_schema (
54
53
action_space : list [Action ] | None ,
55
54
) -> "list[OpenAIFunction] | None" :
56
55
if action_space is None :
57
56
return None
58
- return [OpenAIFunction (action .entry ) for action in action_space ]
57
+ schema_list = []
58
+ for action in action_space :
59
+ new_action = action .to_openai_json_schema ()
60
+ schema = {"type" : "function" , "function" : new_action }
61
+ schema_list .append (OpenAIFunction (action .entry , schema ))
62
+ return schema_list
59
63
60
64
61
65
def _convert_tool_calls_to_action_list (
@@ -84,9 +88,8 @@ def __init__(
84
88
if not CAMEL_ENABLED :
85
89
raise ImportError ("Please install camel-ai to use CamelModel" )
86
90
self .parameters = parameters or {}
87
- # TODO: a better way?
88
- self .model_type = _find_model_type (model )
89
- self .model_platform_type = _find_model_platform_type (model_platform )
91
+ self .model_type = _get_model_type (model )
92
+ self .model_platform_type = _get_model_platform_type (model_platform )
90
93
self .client : ChatAgent | None = None
91
94
self .token_usage = 0
92
95
@@ -104,15 +107,14 @@ def reset(self, system_message: str, action_space: list[Action] | None) -> None:
104
107
config = self .parameters .copy ()
105
108
if action_schema is not None :
106
109
config ["tool_choice" ] = "required"
107
- config ["tools" ] = action_schema
110
+ config ["tools" ] = [
111
+ schema .get_openai_tool_schema () for schema in action_schema
112
+ ]
108
113
109
- chatgpt_config = ChatGPTConfig (
110
- ** config ,
111
- )
112
114
backend_model = ModelFactory .create (
113
115
self .model_platform_type ,
114
116
self .model_type ,
115
- model_config_dict = chatgpt_config . as_dict () ,
117
+ model_config_dict = config ,
116
118
)
117
119
sysmsg = BaseMessage .make_assistant_message (
118
120
role_name = "Assistant" ,
0 commit comments