camel-ai 0.1.5.1__py3-none-any.whl → 0.1.5.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of camel-ai might be problematic. Click here for more details.
- camel/agents/__init__.py +2 -0
- camel/agents/chat_agent.py +237 -52
- camel/agents/critic_agent.py +6 -9
- camel/agents/deductive_reasoner_agent.py +93 -40
- camel/agents/embodied_agent.py +6 -9
- camel/agents/knowledge_graph_agent.py +49 -27
- camel/agents/role_assignment_agent.py +14 -12
- camel/agents/search_agent.py +122 -0
- camel/agents/task_agent.py +26 -38
- camel/bots/__init__.py +20 -0
- camel/bots/discord_bot.py +103 -0
- camel/bots/telegram_bot.py +84 -0
- camel/configs/__init__.py +3 -0
- camel/configs/anthropic_config.py +1 -1
- camel/configs/litellm_config.py +113 -0
- camel/configs/openai_config.py +14 -0
- camel/embeddings/__init__.py +2 -0
- camel/embeddings/openai_embedding.py +2 -2
- camel/embeddings/sentence_transformers_embeddings.py +6 -5
- camel/embeddings/vlm_embedding.py +146 -0
- camel/functions/__init__.py +9 -0
- camel/functions/open_api_function.py +161 -33
- camel/functions/open_api_specs/biztoc/__init__.py +13 -0
- camel/functions/open_api_specs/biztoc/ai-plugin.json +34 -0
- camel/functions/open_api_specs/biztoc/openapi.yaml +21 -0
- camel/functions/open_api_specs/create_qr_code/__init__.py +13 -0
- camel/functions/open_api_specs/create_qr_code/openapi.yaml +44 -0
- camel/functions/open_api_specs/nasa_apod/__init__.py +13 -0
- camel/functions/open_api_specs/nasa_apod/openapi.yaml +72 -0
- camel/functions/open_api_specs/outschool/__init__.py +13 -0
- camel/functions/open_api_specs/outschool/ai-plugin.json +34 -0
- camel/functions/open_api_specs/outschool/openapi.yaml +1 -0
- camel/functions/open_api_specs/outschool/paths/__init__.py +14 -0
- camel/functions/open_api_specs/outschool/paths/get_classes.py +29 -0
- camel/functions/open_api_specs/outschool/paths/search_teachers.py +29 -0
- camel/functions/open_api_specs/security_config.py +21 -0
- camel/functions/open_api_specs/web_scraper/__init__.py +13 -0
- camel/functions/open_api_specs/web_scraper/ai-plugin.json +34 -0
- camel/functions/open_api_specs/web_scraper/openapi.yaml +71 -0
- camel/functions/open_api_specs/web_scraper/paths/__init__.py +13 -0
- camel/functions/open_api_specs/web_scraper/paths/scraper.py +29 -0
- camel/functions/openai_function.py +3 -1
- camel/functions/search_functions.py +104 -171
- camel/functions/slack_functions.py +16 -3
- camel/human.py +3 -1
- camel/loaders/base_io.py +3 -1
- camel/loaders/unstructured_io.py +16 -22
- camel/messages/base.py +135 -46
- camel/models/__init__.py +8 -0
- camel/models/anthropic_model.py +24 -16
- camel/models/base_model.py +6 -1
- camel/models/litellm_model.py +112 -0
- camel/models/model_factory.py +44 -16
- camel/models/nemotron_model.py +71 -0
- camel/models/ollama_model.py +121 -0
- camel/models/open_source_model.py +8 -2
- camel/models/openai_model.py +14 -5
- camel/models/stub_model.py +3 -1
- camel/models/zhipuai_model.py +125 -0
- camel/prompts/__init__.py +6 -0
- camel/prompts/base.py +2 -1
- camel/prompts/descripte_video_prompt.py +33 -0
- camel/prompts/generate_text_embedding_data.py +79 -0
- camel/prompts/task_prompt_template.py +13 -3
- camel/retrievers/auto_retriever.py +20 -11
- camel/retrievers/base.py +4 -2
- camel/retrievers/bm25_retriever.py +2 -1
- camel/retrievers/cohere_rerank_retriever.py +2 -1
- camel/retrievers/vector_retriever.py +10 -4
- camel/societies/babyagi_playing.py +2 -1
- camel/societies/role_playing.py +18 -20
- camel/storages/graph_storages/base.py +1 -0
- camel/storages/graph_storages/neo4j_graph.py +5 -3
- camel/storages/vectordb_storages/base.py +2 -1
- camel/storages/vectordb_storages/milvus.py +5 -2
- camel/toolkits/github_toolkit.py +120 -26
- camel/types/__init__.py +5 -2
- camel/types/enums.py +95 -4
- camel/utils/__init__.py +11 -2
- camel/utils/commons.py +78 -4
- camel/utils/constants.py +26 -0
- camel/utils/token_counting.py +62 -7
- {camel_ai-0.1.5.1.dist-info → camel_ai-0.1.5.3.dist-info}/METADATA +82 -53
- camel_ai-0.1.5.3.dist-info/RECORD +151 -0
- camel_ai-0.1.5.1.dist-info/RECORD +0 -119
- {camel_ai-0.1.5.1.dist-info → camel_ai-0.1.5.3.dist-info}/WHEEL +0 -0
camel/models/model_factory.py
CHANGED
|
@@ -11,14 +11,17 @@
|
|
|
11
11
|
# See the License for the specific language governing permissions and
|
|
12
12
|
# limitations under the License.
|
|
13
13
|
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
-
from typing import Any, Dict, Optional
|
|
14
|
+
from typing import Any, Dict, Optional, Union
|
|
15
15
|
|
|
16
16
|
from camel.models.anthropic_model import AnthropicModel
|
|
17
17
|
from camel.models.base_model import BaseModelBackend
|
|
18
|
+
from camel.models.litellm_model import LiteLLMModel
|
|
19
|
+
from camel.models.ollama_model import OllamaModel
|
|
18
20
|
from camel.models.open_source_model import OpenSourceModel
|
|
19
21
|
from camel.models.openai_model import OpenAIModel
|
|
20
22
|
from camel.models.stub_model import StubModel
|
|
21
|
-
from camel.
|
|
23
|
+
from camel.models.zhipuai_model import ZhipuAIModel
|
|
24
|
+
from camel.types import ModelPlatformType, ModelType
|
|
22
25
|
|
|
23
26
|
|
|
24
27
|
class ModelFactory:
|
|
@@ -30,18 +33,24 @@ class ModelFactory:
|
|
|
30
33
|
|
|
31
34
|
@staticmethod
|
|
32
35
|
def create(
|
|
33
|
-
|
|
36
|
+
model_platform: ModelPlatformType,
|
|
37
|
+
model_type: Union[ModelType, str],
|
|
34
38
|
model_config_dict: Dict,
|
|
35
39
|
api_key: Optional[str] = None,
|
|
40
|
+
url: Optional[str] = None,
|
|
36
41
|
) -> BaseModelBackend:
|
|
37
42
|
r"""Creates an instance of `BaseModelBackend` of the specified type.
|
|
38
43
|
|
|
39
44
|
Args:
|
|
40
|
-
|
|
45
|
+
model_platform (ModelPlatformType): Platform from which the model
|
|
46
|
+
originates.
|
|
47
|
+
model_type (Union[ModelType, str]): Model for which a backend is
|
|
48
|
+
created can be a `str` for open source platforms.
|
|
41
49
|
model_config_dict (Dict): A dictionary that will be fed into
|
|
42
50
|
the backend constructor.
|
|
43
51
|
api_key (Optional[str]): The API key for authenticating with the
|
|
44
|
-
|
|
52
|
+
model service.
|
|
53
|
+
url (Optional[str]): The url to the model service.
|
|
45
54
|
|
|
46
55
|
Raises:
|
|
47
56
|
ValueError: If there is not backend for the model.
|
|
@@ -50,16 +59,35 @@ class ModelFactory:
|
|
|
50
59
|
BaseModelBackend: The initialized backend.
|
|
51
60
|
"""
|
|
52
61
|
model_class: Any
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
|
|
63
|
+
if isinstance(model_type, ModelType):
|
|
64
|
+
if model_platform.is_open_source and model_type.is_open_source:
|
|
65
|
+
model_class = OpenSourceModel
|
|
66
|
+
return model_class(model_type, model_config_dict, url)
|
|
67
|
+
if model_platform.is_openai and model_type.is_openai:
|
|
68
|
+
model_class = OpenAIModel
|
|
69
|
+
elif model_platform.is_anthropic and model_type.is_anthropic:
|
|
70
|
+
model_class = AnthropicModel
|
|
71
|
+
elif model_platform.is_zhipuai and model_type.is_zhipuai:
|
|
72
|
+
model_class = ZhipuAIModel
|
|
73
|
+
elif model_type == ModelType.STUB:
|
|
74
|
+
model_class = StubModel
|
|
75
|
+
else:
|
|
76
|
+
raise ValueError(
|
|
77
|
+
f"Unknown pair of model platform `{model_platform}` "
|
|
78
|
+
f"and model type `{model_type}`."
|
|
79
|
+
)
|
|
80
|
+
elif isinstance(model_type, str):
|
|
81
|
+
if model_platform.is_ollama:
|
|
82
|
+
model_class = OllamaModel
|
|
83
|
+
elif model_platform.is_litellm:
|
|
84
|
+
model_class = LiteLLMModel
|
|
85
|
+
else:
|
|
86
|
+
raise ValueError(
|
|
87
|
+
f"Unknown pair of model platform `{model_platform}` "
|
|
88
|
+
f"and model type `{model_type}`."
|
|
89
|
+
)
|
|
61
90
|
else:
|
|
62
|
-
raise ValueError(f"
|
|
91
|
+
raise ValueError(f"Invalid model type `{model_type}` provided.")
|
|
63
92
|
|
|
64
|
-
|
|
65
|
-
return inst
|
|
93
|
+
return model_class(model_type, model_config_dict, api_key, url)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the “License”);
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an “AS IS” BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
+
import os
|
|
15
|
+
from typing import List, Optional
|
|
16
|
+
|
|
17
|
+
from openai import OpenAI
|
|
18
|
+
|
|
19
|
+
from camel.messages import OpenAIMessage
|
|
20
|
+
from camel.types import ChatCompletion, ModelType
|
|
21
|
+
from camel.utils import (
|
|
22
|
+
BaseTokenCounter,
|
|
23
|
+
model_api_key_required,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class NemotronModel:
|
|
28
|
+
r"""Nemotron model API backend with OpenAI compatibility."""
|
|
29
|
+
|
|
30
|
+
# NOTE: Nemotron model doesn't support additional model config like OpenAI.
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
model_type: ModelType,
|
|
35
|
+
api_key: Optional[str] = None,
|
|
36
|
+
) -> None:
|
|
37
|
+
r"""Constructor for Nvidia backend.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
model_type (ModelType): Model for which a backend is created.
|
|
41
|
+
api_key (Optional[str]): The API key for authenticating with the
|
|
42
|
+
Nvidia service. (default: :obj:`None`)
|
|
43
|
+
"""
|
|
44
|
+
self.model_type = model_type
|
|
45
|
+
url = os.environ.get('NVIDIA_API_BASE_URL', None)
|
|
46
|
+
self._api_key = api_key or os.environ.get("NVIDIA_API_KEY")
|
|
47
|
+
if not url or not self._api_key:
|
|
48
|
+
raise ValueError("The NVIDIA API base url and key should be set.")
|
|
49
|
+
self._client = OpenAI(
|
|
50
|
+
timeout=60, max_retries=3, base_url=url, api_key=self._api_key
|
|
51
|
+
)
|
|
52
|
+
self._token_counter: Optional[BaseTokenCounter] = None
|
|
53
|
+
|
|
54
|
+
@model_api_key_required
|
|
55
|
+
def run(
|
|
56
|
+
self,
|
|
57
|
+
messages: List[OpenAIMessage],
|
|
58
|
+
) -> ChatCompletion:
|
|
59
|
+
r"""Runs inference of OpenAI chat completion.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
messages (List[OpenAIMessage]): Message list.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
ChatCompletion.
|
|
66
|
+
"""
|
|
67
|
+
response = self._client.chat.completions.create(
|
|
68
|
+
messages=messages,
|
|
69
|
+
model=self.model_type.value,
|
|
70
|
+
)
|
|
71
|
+
return response
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the “License”);
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an “AS IS” BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
+
import os
|
|
15
|
+
from typing import Any, Dict, List, Optional, Union
|
|
16
|
+
|
|
17
|
+
from openai import OpenAI, Stream
|
|
18
|
+
|
|
19
|
+
from camel.configs import OPENAI_API_PARAMS
|
|
20
|
+
from camel.messages import OpenAIMessage
|
|
21
|
+
from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
|
|
22
|
+
from camel.utils import BaseTokenCounter, OpenAITokenCounter
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class OllamaModel:
|
|
26
|
+
r"""Ollama service interface."""
|
|
27
|
+
|
|
28
|
+
# NOTE: Current `ModelType and `TokenCounter` desigen is not suitable,
|
|
29
|
+
# stream mode is not supported
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
model_type: str,
|
|
34
|
+
model_config_dict: Dict[str, Any],
|
|
35
|
+
api_key: Optional[str] = None,
|
|
36
|
+
url: Optional[str] = None,
|
|
37
|
+
) -> None:
|
|
38
|
+
r"""Constructor for Ollama backend with OpenAI compatibility.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
model_type (str): Model for which a backend is created.
|
|
42
|
+
model_config_dict (Dict[str, Any]): A dictionary that will
|
|
43
|
+
be fed into openai.ChatCompletion.create().
|
|
44
|
+
api_key (Optional[str]): The API key for authenticating with the
|
|
45
|
+
model service. (default: :obj:`None`)
|
|
46
|
+
url (Optional[str]): The url to the model service.
|
|
47
|
+
"""
|
|
48
|
+
self.model_type = model_type
|
|
49
|
+
self.model_config_dict = model_config_dict
|
|
50
|
+
self._url = url or os.environ.get('OPENAI_API_BASE_URL')
|
|
51
|
+
self._api_key = api_key or os.environ.get("OPENAI_API_KEY")
|
|
52
|
+
# Use OpenAI cilent as interface call Ollama
|
|
53
|
+
# Reference: https://github.com/ollama/ollama/blob/main/docs/openai.md
|
|
54
|
+
self._client = OpenAI(
|
|
55
|
+
timeout=60,
|
|
56
|
+
max_retries=3,
|
|
57
|
+
base_url=self._url,
|
|
58
|
+
api_key=self._api_key,
|
|
59
|
+
)
|
|
60
|
+
self._token_counter: Optional[BaseTokenCounter] = None
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def token_counter(self) -> BaseTokenCounter:
|
|
64
|
+
r"""Initialize the token counter for the model backend.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
BaseTokenCounter: The token counter following the model's
|
|
68
|
+
tokenization style.
|
|
69
|
+
"""
|
|
70
|
+
# NOTE: Use OpenAITokenCounter temporarily
|
|
71
|
+
if not self._token_counter:
|
|
72
|
+
self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)
|
|
73
|
+
return self._token_counter
|
|
74
|
+
|
|
75
|
+
def check_model_config(self):
|
|
76
|
+
r"""Check whether the model configuration contains any
|
|
77
|
+
unexpected arguments to OpenAI API.
|
|
78
|
+
|
|
79
|
+
Raises:
|
|
80
|
+
ValueError: If the model configuration dictionary contains any
|
|
81
|
+
unexpected arguments to OpenAI API.
|
|
82
|
+
"""
|
|
83
|
+
for param in self.model_config_dict:
|
|
84
|
+
if param not in OPENAI_API_PARAMS:
|
|
85
|
+
raise ValueError(
|
|
86
|
+
f"Unexpected argument `{param}` is "
|
|
87
|
+
"input into OpenAI model backend."
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
def run(
|
|
91
|
+
self,
|
|
92
|
+
messages: List[OpenAIMessage],
|
|
93
|
+
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
|
|
94
|
+
r"""Runs inference of OpenAI chat completion.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
messages (List[OpenAIMessage]): Message list with the chat history
|
|
98
|
+
in OpenAI API format.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
Union[ChatCompletion, Stream[ChatCompletionChunk]]:
|
|
102
|
+
`ChatCompletion` in the non-stream mode, or
|
|
103
|
+
`Stream[ChatCompletionChunk]` in the stream mode.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
response = self._client.chat.completions.create(
|
|
107
|
+
messages=messages,
|
|
108
|
+
model=self.model_type,
|
|
109
|
+
**self.model_config_dict,
|
|
110
|
+
)
|
|
111
|
+
return response
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def stream(self) -> bool:
|
|
115
|
+
r"""Returns whether the model is in stream mode, which sends partial
|
|
116
|
+
results each time.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
bool: Whether the model is in stream mode.
|
|
120
|
+
"""
|
|
121
|
+
return self.model_config_dict.get('stream', False)
|
|
@@ -31,6 +31,8 @@ class OpenSourceModel(BaseModelBackend):
|
|
|
31
31
|
self,
|
|
32
32
|
model_type: ModelType,
|
|
33
33
|
model_config_dict: Dict[str, Any],
|
|
34
|
+
api_key: Optional[str] = None,
|
|
35
|
+
url: Optional[str] = None,
|
|
34
36
|
) -> None:
|
|
35
37
|
r"""Constructor for model backends of Open-source models.
|
|
36
38
|
|
|
@@ -38,8 +40,11 @@ class OpenSourceModel(BaseModelBackend):
|
|
|
38
40
|
model_type (ModelType): Model for which a backend is created.
|
|
39
41
|
model_config_dict (Dict[str, Any]): A dictionary that will
|
|
40
42
|
be fed into :obj:`openai.ChatCompletion.create()`.
|
|
43
|
+
api_key (Optional[str]): The API key for authenticating with the
|
|
44
|
+
model service. (ignored for open-source models)
|
|
45
|
+
url (Optional[str]): The url to the model service.
|
|
41
46
|
"""
|
|
42
|
-
super().__init__(model_type, model_config_dict)
|
|
47
|
+
super().__init__(model_type, model_config_dict, api_key, url)
|
|
43
48
|
self._token_counter: Optional[BaseTokenCounter] = None
|
|
44
49
|
|
|
45
50
|
# Check whether the input model type is open-source
|
|
@@ -65,7 +70,7 @@ class OpenSourceModel(BaseModelBackend):
|
|
|
65
70
|
)
|
|
66
71
|
|
|
67
72
|
# Load the server URL and check whether it is None
|
|
68
|
-
server_url: Optional[str] = self.model_config_dict.get(
|
|
73
|
+
server_url: Optional[str] = url or self.model_config_dict.get(
|
|
69
74
|
"server_url", None
|
|
70
75
|
)
|
|
71
76
|
if not server_url:
|
|
@@ -150,6 +155,7 @@ class OpenSourceModel(BaseModelBackend):
|
|
|
150
155
|
def stream(self) -> bool:
|
|
151
156
|
r"""Returns whether the model is in stream mode,
|
|
152
157
|
which sends partial results each time.
|
|
158
|
+
|
|
153
159
|
Returns:
|
|
154
160
|
bool: Whether the model is in stream mode.
|
|
155
161
|
"""
|
camel/models/openai_model.py
CHANGED
|
@@ -20,7 +20,11 @@ from camel.configs import OPENAI_API_PARAMS
|
|
|
20
20
|
from camel.messages import OpenAIMessage
|
|
21
21
|
from camel.models import BaseModelBackend
|
|
22
22
|
from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
|
|
23
|
-
from camel.utils import
|
|
23
|
+
from camel.utils import (
|
|
24
|
+
BaseTokenCounter,
|
|
25
|
+
OpenAITokenCounter,
|
|
26
|
+
model_api_key_required,
|
|
27
|
+
)
|
|
24
28
|
|
|
25
29
|
|
|
26
30
|
class OpenAIModel(BaseModelBackend):
|
|
@@ -31,6 +35,7 @@ class OpenAIModel(BaseModelBackend):
|
|
|
31
35
|
model_type: ModelType,
|
|
32
36
|
model_config_dict: Dict[str, Any],
|
|
33
37
|
api_key: Optional[str] = None,
|
|
38
|
+
url: Optional[str] = None,
|
|
34
39
|
) -> None:
|
|
35
40
|
r"""Constructor for OpenAI backend.
|
|
36
41
|
|
|
@@ -41,12 +46,16 @@ class OpenAIModel(BaseModelBackend):
|
|
|
41
46
|
be fed into openai.ChatCompletion.create().
|
|
42
47
|
api_key (Optional[str]): The API key for authenticating with the
|
|
43
48
|
OpenAI service. (default: :obj:`None`)
|
|
49
|
+
url (Optional[str]): The url to the OpenAI service.
|
|
44
50
|
"""
|
|
45
|
-
super().__init__(model_type, model_config_dict)
|
|
46
|
-
|
|
51
|
+
super().__init__(model_type, model_config_dict, api_key, url)
|
|
52
|
+
self._url = url or os.environ.get("OPENAI_API_BASE_URL")
|
|
47
53
|
self._api_key = api_key or os.environ.get("OPENAI_API_KEY")
|
|
48
54
|
self._client = OpenAI(
|
|
49
|
-
timeout=60,
|
|
55
|
+
timeout=60,
|
|
56
|
+
max_retries=3,
|
|
57
|
+
base_url=self._url,
|
|
58
|
+
api_key=self._api_key,
|
|
50
59
|
)
|
|
51
60
|
self._token_counter: Optional[BaseTokenCounter] = None
|
|
52
61
|
|
|
@@ -62,7 +71,7 @@ class OpenAIModel(BaseModelBackend):
|
|
|
62
71
|
self._token_counter = OpenAITokenCounter(self.model_type)
|
|
63
72
|
return self._token_counter
|
|
64
73
|
|
|
65
|
-
@
|
|
74
|
+
@model_api_key_required
|
|
66
75
|
def run(
|
|
67
76
|
self,
|
|
68
77
|
messages: List[OpenAIMessage],
|
camel/models/stub_model.py
CHANGED
|
@@ -54,11 +54,13 @@ class StubModel(BaseModelBackend):
|
|
|
54
54
|
model_type: ModelType,
|
|
55
55
|
model_config_dict: Dict[str, Any],
|
|
56
56
|
api_key: Optional[str] = None,
|
|
57
|
+
url: Optional[str] = None,
|
|
57
58
|
) -> None:
|
|
58
59
|
r"""All arguments are unused for the dummy model."""
|
|
59
|
-
super().__init__(model_type, model_config_dict)
|
|
60
|
+
super().__init__(model_type, model_config_dict, api_key, url)
|
|
60
61
|
self._token_counter: Optional[BaseTokenCounter] = None
|
|
61
62
|
self._api_key = api_key
|
|
63
|
+
self._url = url
|
|
62
64
|
|
|
63
65
|
@property
|
|
64
66
|
def token_counter(self) -> BaseTokenCounter:
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the “License”);
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an “AS IS” BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
+
|
|
15
|
+
import os
|
|
16
|
+
from typing import Any, Dict, List, Optional, Union
|
|
17
|
+
|
|
18
|
+
from openai import OpenAI, Stream
|
|
19
|
+
|
|
20
|
+
from camel.configs import OPENAI_API_PARAMS
|
|
21
|
+
from camel.messages import OpenAIMessage
|
|
22
|
+
from camel.models import BaseModelBackend
|
|
23
|
+
from camel.types import ChatCompletion, ChatCompletionChunk, ModelType
|
|
24
|
+
from camel.utils import (
|
|
25
|
+
BaseTokenCounter,
|
|
26
|
+
OpenAITokenCounter,
|
|
27
|
+
model_api_key_required,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ZhipuAIModel(BaseModelBackend):
|
|
32
|
+
r"""ZhipuAI API in a unified BaseModelBackend interface."""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
model_type: ModelType,
|
|
37
|
+
model_config_dict: Dict[str, Any],
|
|
38
|
+
api_key: Optional[str] = None,
|
|
39
|
+
url: Optional[str] = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
r"""Constructor for ZhipuAI backend.
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
model_type (ModelType): Model for which a backend is created,
|
|
45
|
+
such as GLM_* series.
|
|
46
|
+
model_config_dict (Dict[str, Any]): A dictionary that will
|
|
47
|
+
be fed into openai.ChatCompletion.create().
|
|
48
|
+
api_key (Optional[str]): The API key for authenticating with the
|
|
49
|
+
ZhipuAI service. (default: :obj:`None`)
|
|
50
|
+
"""
|
|
51
|
+
super().__init__(model_type, model_config_dict)
|
|
52
|
+
self._url = url or os.environ.get("ZHIPUAI_API_BASE_URL")
|
|
53
|
+
self._api_key = api_key or os.environ.get("ZHIPUAI_API_KEY")
|
|
54
|
+
self._client = OpenAI(
|
|
55
|
+
timeout=60,
|
|
56
|
+
max_retries=3,
|
|
57
|
+
api_key=self._api_key,
|
|
58
|
+
base_url=self._url,
|
|
59
|
+
)
|
|
60
|
+
self._token_counter: Optional[BaseTokenCounter] = None
|
|
61
|
+
|
|
62
|
+
@model_api_key_required
|
|
63
|
+
def run(
|
|
64
|
+
self,
|
|
65
|
+
messages: List[OpenAIMessage],
|
|
66
|
+
) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]:
|
|
67
|
+
r"""Runs inference of OpenAI chat completion.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
messages (List[OpenAIMessage]): Message list with the chat history
|
|
71
|
+
in OpenAI API format.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
Union[ChatCompletion, Stream[ChatCompletionChunk]]:
|
|
75
|
+
`ChatCompletion` in the non-stream mode, or
|
|
76
|
+
`Stream[ChatCompletionChunk]` in the stream mode.
|
|
77
|
+
"""
|
|
78
|
+
# Use OpenAI cilent as interface call ZhipuAI
|
|
79
|
+
# Reference: https://open.bigmodel.cn/dev/api#openai_sdk
|
|
80
|
+
response = self._client.chat.completions.create(
|
|
81
|
+
messages=messages,
|
|
82
|
+
model=self.model_type.value,
|
|
83
|
+
**self.model_config_dict,
|
|
84
|
+
)
|
|
85
|
+
return response
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def token_counter(self) -> BaseTokenCounter:
|
|
89
|
+
r"""Initialize the token counter for the model backend.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
OpenAITokenCounter: The token counter following the model's
|
|
93
|
+
tokenization style.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
if not self._token_counter:
|
|
97
|
+
# It's a temporary setting for token counter.
|
|
98
|
+
self._token_counter = OpenAITokenCounter(ModelType.GPT_3_5_TURBO)
|
|
99
|
+
return self._token_counter
|
|
100
|
+
|
|
101
|
+
def check_model_config(self):
|
|
102
|
+
r"""Check whether the model configuration contains any
|
|
103
|
+
unexpected arguments to OpenAI API.
|
|
104
|
+
|
|
105
|
+
Raises:
|
|
106
|
+
ValueError: If the model configuration dictionary contains any
|
|
107
|
+
unexpected arguments to OpenAI API.
|
|
108
|
+
"""
|
|
109
|
+
for param in self.model_config_dict:
|
|
110
|
+
if param not in OPENAI_API_PARAMS:
|
|
111
|
+
raise ValueError(
|
|
112
|
+
f"Unexpected argument `{param}` is "
|
|
113
|
+
"input into OpenAI model backend."
|
|
114
|
+
)
|
|
115
|
+
pass
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def stream(self) -> bool:
|
|
119
|
+
r"""Returns whether the model is in stream mode, which sends partial
|
|
120
|
+
results each time.
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
bool: Whether the model is in stream mode.
|
|
124
|
+
"""
|
|
125
|
+
return self.model_config_dict.get('stream', False)
|
camel/prompts/__init__.py
CHANGED
|
@@ -14,7 +14,11 @@
|
|
|
14
14
|
from .ai_society import AISocietyPromptTemplateDict
|
|
15
15
|
from .base import CodePrompt, TextPrompt, TextPromptDict
|
|
16
16
|
from .code import CodePromptTemplateDict
|
|
17
|
+
from .descripte_video_prompt import DescriptionVideoPromptTemplateDict
|
|
17
18
|
from .evaluation import EvaluationPromptTemplateDict
|
|
19
|
+
from .generate_text_embedding_data import (
|
|
20
|
+
GenerateTextEmbeddingDataPromptTemplateDict,
|
|
21
|
+
)
|
|
18
22
|
from .misalignment import MisalignmentPromptTemplateDict
|
|
19
23
|
from .object_recognition import ObjectRecognitionPromptTemplateDict
|
|
20
24
|
from .prompt_templates import PromptTemplateGenerator
|
|
@@ -36,5 +40,7 @@ __all__ = [
|
|
|
36
40
|
'TaskPromptTemplateDict',
|
|
37
41
|
'PromptTemplateGenerator',
|
|
38
42
|
'SolutionExtractionPromptTemplateDict',
|
|
43
|
+
'GenerateTextEmbeddingDataPromptTemplateDict',
|
|
39
44
|
'ObjectRecognitionPromptTemplateDict',
|
|
45
|
+
'DescriptionVideoPromptTemplateDict',
|
|
40
46
|
]
|
camel/prompts/base.py
CHANGED
|
@@ -208,7 +208,8 @@ class TextPromptDict(Dict[Any, TextPrompt]):
|
|
|
208
208
|
EMBODIMENT_PROMPT = TextPrompt(
|
|
209
209
|
"System information :"
|
|
210
210
|
+ "\n".join(
|
|
211
|
-
f"{key}: {value}"
|
|
211
|
+
f"{key}: {value}"
|
|
212
|
+
for key, value in get_system_information().items()
|
|
212
213
|
)
|
|
213
214
|
+ "\n"
|
|
214
215
|
+ """You are the physical embodiment of the {role} who is working on solving a task: {task}.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the “License”);
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an “AS IS” BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from camel.prompts.base import TextPrompt, TextPromptDict
|
|
17
|
+
from camel.types import RoleType
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# flake8: noqa :E501
|
|
21
|
+
class DescriptionVideoPromptTemplateDict(TextPromptDict):
|
|
22
|
+
ASSISTANT_PROMPT = TextPrompt(
|
|
23
|
+
"""You are a master of video analysis.
|
|
24
|
+
Please provide a shot description of the content of the current video."""
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
28
|
+
super().__init__(*args, **kwargs)
|
|
29
|
+
self.update(
|
|
30
|
+
{
|
|
31
|
+
RoleType.ASSISTANT: self.ASSISTANT_PROMPT,
|
|
32
|
+
}
|
|
33
|
+
)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the “License”);
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an “AS IS” BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from camel.prompts import TextPrompt, TextPromptDict
|
|
17
|
+
from camel.types import RoleType
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# flake8: noqa :E501
|
|
21
|
+
class GenerateTextEmbeddingDataPromptTemplateDict(TextPromptDict):
|
|
22
|
+
r"""A :obj:`TextPrompt` dictionary containing text embedding tasks
|
|
23
|
+
generation, query, positive and hard negative samples generation,
|
|
24
|
+
from the `"Improving Text Embeddings with Large Language Models"
|
|
25
|
+
<https://arxiv.org/abs/2401.00368>`_ paper.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
Attributes:
|
|
29
|
+
GENERATE_TASKS (TextPrompt): A prompt to generate a list
|
|
30
|
+
of :obj:`num_tasks` synthetic text_embedding tasks.
|
|
31
|
+
ASSISTANT_PROMPT (TextPrompt): A system prompt for the AI assistant
|
|
32
|
+
to generate synthetic :obj:`user_query`, :obj:`positive document`,
|
|
33
|
+
and :obj:`hard_negative_document` for a specific :obj:`task` with
|
|
34
|
+
specified parameters including :obj:`query_type`,
|
|
35
|
+
:obj:`query_length`, :obj:`clarity`, :obj:`num_words`,
|
|
36
|
+
:obj:`language` and :obj:`difficulty`.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
GENERATE_TASKS = TextPrompt(
|
|
40
|
+
"""You are an expert to brainstorm a list of {num_tasks} potentially useful text retrieval tasks
|
|
41
|
+
Here are a few examples for your reference:
|
|
42
|
+
- Provided a scientific claim as query, retrieve documents that help verify or refute the claim.
|
|
43
|
+
- Search for documents that answers a FAQ-style query on children's nutrition.
|
|
44
|
+
Please adhere to the following guidelines:
|
|
45
|
+
- Specify what the query is, and what the desired documents are.
|
|
46
|
+
- Each retrieval task should cover a wide range of queries, and should not be too specific.
|
|
47
|
+
Your output should always be a python list of strings starting with `1.`, `2.` etc.
|
|
48
|
+
And each element corresponds to a distinct retrieval task in one sentence.
|
|
49
|
+
Do not explain yourself or output anything else.
|
|
50
|
+
Be creative!"""
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
ASSISTANT_PROMPT = TextPrompt(
|
|
54
|
+
"""You have been assigned a retrieval task: {task}
|
|
55
|
+
Your mission is to write one text retrieval example for this task in JSON format. The JSON object must
|
|
56
|
+
contain the following keys:
|
|
57
|
+
- "user_query": a string, a random user search query specified by the retrieval task.
|
|
58
|
+
- "positive_document": a string, a relevant document for the user query.
|
|
59
|
+
- "hard_negative_document": a string, a hard negative document that only appears relevant to the query.
|
|
60
|
+
Please adhere to the following guidelines:
|
|
61
|
+
- The "user_query" should be {query_type}, {query_length}, {clarity}, and diverse in topic.
|
|
62
|
+
- All documents must be created independent of the query. Avoid copying the query verbatim.
|
|
63
|
+
It's acceptable if some parts of the "positive_document" are not topically related to the query.
|
|
64
|
+
- All documents should be at least {num_words} words long.
|
|
65
|
+
- The "hard_negative_document" contains some useful information, but it should be less useful or comprehensive compared to the "positive_document".
|
|
66
|
+
- Both the query and documents should be in {language}.
|
|
67
|
+
- Do not provide any explanation in any document on why it is relevant or not relevant to the query.
|
|
68
|
+
- Both the query and documents require {difficulty} level education to understand.
|
|
69
|
+
Your output must always be a JSON object only (starting and ending with curly brackets), do not explain yourself or output anything else. Be creative!"""
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
73
|
+
super().__init__(*args, **kwargs)
|
|
74
|
+
self.update(
|
|
75
|
+
{
|
|
76
|
+
"generate_tasks": self.GENERATE_TASKS,
|
|
77
|
+
RoleType.ASSISTANT: self.ASSISTANT_PROMPT,
|
|
78
|
+
}
|
|
79
|
+
)
|