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.

Files changed (86) hide show
  1. camel/agents/__init__.py +2 -0
  2. camel/agents/chat_agent.py +237 -52
  3. camel/agents/critic_agent.py +6 -9
  4. camel/agents/deductive_reasoner_agent.py +93 -40
  5. camel/agents/embodied_agent.py +6 -9
  6. camel/agents/knowledge_graph_agent.py +49 -27
  7. camel/agents/role_assignment_agent.py +14 -12
  8. camel/agents/search_agent.py +122 -0
  9. camel/agents/task_agent.py +26 -38
  10. camel/bots/__init__.py +20 -0
  11. camel/bots/discord_bot.py +103 -0
  12. camel/bots/telegram_bot.py +84 -0
  13. camel/configs/__init__.py +3 -0
  14. camel/configs/anthropic_config.py +1 -1
  15. camel/configs/litellm_config.py +113 -0
  16. camel/configs/openai_config.py +14 -0
  17. camel/embeddings/__init__.py +2 -0
  18. camel/embeddings/openai_embedding.py +2 -2
  19. camel/embeddings/sentence_transformers_embeddings.py +6 -5
  20. camel/embeddings/vlm_embedding.py +146 -0
  21. camel/functions/__init__.py +9 -0
  22. camel/functions/open_api_function.py +161 -33
  23. camel/functions/open_api_specs/biztoc/__init__.py +13 -0
  24. camel/functions/open_api_specs/biztoc/ai-plugin.json +34 -0
  25. camel/functions/open_api_specs/biztoc/openapi.yaml +21 -0
  26. camel/functions/open_api_specs/create_qr_code/__init__.py +13 -0
  27. camel/functions/open_api_specs/create_qr_code/openapi.yaml +44 -0
  28. camel/functions/open_api_specs/nasa_apod/__init__.py +13 -0
  29. camel/functions/open_api_specs/nasa_apod/openapi.yaml +72 -0
  30. camel/functions/open_api_specs/outschool/__init__.py +13 -0
  31. camel/functions/open_api_specs/outschool/ai-plugin.json +34 -0
  32. camel/functions/open_api_specs/outschool/openapi.yaml +1 -0
  33. camel/functions/open_api_specs/outschool/paths/__init__.py +14 -0
  34. camel/functions/open_api_specs/outschool/paths/get_classes.py +29 -0
  35. camel/functions/open_api_specs/outschool/paths/search_teachers.py +29 -0
  36. camel/functions/open_api_specs/security_config.py +21 -0
  37. camel/functions/open_api_specs/web_scraper/__init__.py +13 -0
  38. camel/functions/open_api_specs/web_scraper/ai-plugin.json +34 -0
  39. camel/functions/open_api_specs/web_scraper/openapi.yaml +71 -0
  40. camel/functions/open_api_specs/web_scraper/paths/__init__.py +13 -0
  41. camel/functions/open_api_specs/web_scraper/paths/scraper.py +29 -0
  42. camel/functions/openai_function.py +3 -1
  43. camel/functions/search_functions.py +104 -171
  44. camel/functions/slack_functions.py +16 -3
  45. camel/human.py +3 -1
  46. camel/loaders/base_io.py +3 -1
  47. camel/loaders/unstructured_io.py +16 -22
  48. camel/messages/base.py +135 -46
  49. camel/models/__init__.py +8 -0
  50. camel/models/anthropic_model.py +24 -16
  51. camel/models/base_model.py +6 -1
  52. camel/models/litellm_model.py +112 -0
  53. camel/models/model_factory.py +44 -16
  54. camel/models/nemotron_model.py +71 -0
  55. camel/models/ollama_model.py +121 -0
  56. camel/models/open_source_model.py +8 -2
  57. camel/models/openai_model.py +14 -5
  58. camel/models/stub_model.py +3 -1
  59. camel/models/zhipuai_model.py +125 -0
  60. camel/prompts/__init__.py +6 -0
  61. camel/prompts/base.py +2 -1
  62. camel/prompts/descripte_video_prompt.py +33 -0
  63. camel/prompts/generate_text_embedding_data.py +79 -0
  64. camel/prompts/task_prompt_template.py +13 -3
  65. camel/retrievers/auto_retriever.py +20 -11
  66. camel/retrievers/base.py +4 -2
  67. camel/retrievers/bm25_retriever.py +2 -1
  68. camel/retrievers/cohere_rerank_retriever.py +2 -1
  69. camel/retrievers/vector_retriever.py +10 -4
  70. camel/societies/babyagi_playing.py +2 -1
  71. camel/societies/role_playing.py +18 -20
  72. camel/storages/graph_storages/base.py +1 -0
  73. camel/storages/graph_storages/neo4j_graph.py +5 -3
  74. camel/storages/vectordb_storages/base.py +2 -1
  75. camel/storages/vectordb_storages/milvus.py +5 -2
  76. camel/toolkits/github_toolkit.py +120 -26
  77. camel/types/__init__.py +5 -2
  78. camel/types/enums.py +95 -4
  79. camel/utils/__init__.py +11 -2
  80. camel/utils/commons.py +78 -4
  81. camel/utils/constants.py +26 -0
  82. camel/utils/token_counting.py +62 -7
  83. {camel_ai-0.1.5.1.dist-info → camel_ai-0.1.5.3.dist-info}/METADATA +82 -53
  84. camel_ai-0.1.5.3.dist-info/RECORD +151 -0
  85. camel_ai-0.1.5.1.dist-info/RECORD +0 -119
  86. {camel_ai-0.1.5.1.dist-info → camel_ai-0.1.5.3.dist-info}/WHEEL +0 -0
@@ -0,0 +1,103 @@
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 TYPE_CHECKING, List, Optional
16
+
17
+ from camel.agents import ChatAgent
18
+ from camel.messages import BaseMessage
19
+
20
+ if TYPE_CHECKING:
21
+ from discord import Message
22
+
23
+
24
+ class DiscordBot:
25
+ r"""Represents a Discord bot that is powered by an agent.
26
+
27
+ Attributes:
28
+ chat_agent (ChatAgent): Chat agent that will power the bot.
29
+ channel_ids (List[int], optional): The channel IDs that the bot will
30
+ listen to.
31
+ discord_token (str, optional): The bot token.
32
+ """
33
+
34
+ def __init__(
35
+ self,
36
+ chat_agent: ChatAgent,
37
+ channel_ids: Optional[List[int]] = None,
38
+ discord_token: Optional[str] = None,
39
+ ) -> None:
40
+ self.chat_agent = chat_agent
41
+ self.token = discord_token or os.getenv('DISCORD_TOKEN')
42
+ self.channel_ids = channel_ids
43
+
44
+ if not self.token:
45
+ raise ValueError(
46
+ "`DISCORD_TOKEN` not found in environment variables. Get it"
47
+ " here: `https://discord.com/developers/applications`."
48
+ )
49
+
50
+ try:
51
+ import discord
52
+ except ImportError:
53
+ raise ImportError(
54
+ "Please install `discord` first. You can install it by running"
55
+ " `python3 -m pip install -U discord.py`."
56
+ )
57
+ intents = discord.Intents.default()
58
+ intents.message_content = True
59
+ self.client = discord.Client(intents=intents)
60
+
61
+ # Register event handlers
62
+ self.client.event(self.on_ready)
63
+ self.client.event(self.on_message)
64
+
65
+ def run(self) -> None:
66
+ r"""Start the Discord bot using its token.
67
+
68
+ This method starts the Discord bot by running the client with the
69
+ provided token.
70
+ """
71
+ self.client.run(self.token) # type: ignore[arg-type]
72
+
73
+ async def on_ready(self) -> None:
74
+ r"""This method is called when the bot has successfully connected to
75
+ the Discord server.
76
+
77
+ It prints a message indicating that the bot has logged in and displays
78
+ the username of the bot.
79
+ """
80
+ print(f'We have logged in as {self.client.user}')
81
+
82
+ async def on_message(self, message: 'Message') -> None:
83
+ r"""Event handler for when a message is received.
84
+
85
+ Args:
86
+ message (discord.Message): The message object received.
87
+ """
88
+ if message.author == self.client.user:
89
+ return
90
+
91
+ if self.channel_ids and message.channel.id not in self.channel_ids:
92
+ return
93
+
94
+ if not self.client.user or not self.client.user.mentioned_in(message):
95
+ return
96
+
97
+ self.chat_agent.reset()
98
+
99
+ user_msg = BaseMessage.make_user_message(
100
+ role_name="User", content=message.content
101
+ )
102
+ assistant_response = self.chat_agent.step(user_msg)
103
+ await message.channel.send(assistant_response.msg.content)
@@ -0,0 +1,84 @@
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 TYPE_CHECKING, Optional
16
+
17
+ from camel.agents import ChatAgent
18
+ from camel.messages import BaseMessage
19
+
20
+ # Conditionally import telebot types only for type checking
21
+ if TYPE_CHECKING:
22
+ from telebot.types import Message # type: ignore[import-untyped]
23
+
24
+
25
+ class TelegramBot:
26
+ r"""Represents a Telegram bot that is powered by an agent.
27
+
28
+ Attributes:
29
+ chat_agent (ChatAgent): Chat agent that will power the bot.
30
+ telegram_token (str, optional): The bot token.
31
+ """
32
+
33
+ def __init__(
34
+ self,
35
+ chat_agent: ChatAgent,
36
+ telegram_token: Optional[str] = None,
37
+ ) -> None:
38
+ self.chat_agent = chat_agent
39
+
40
+ if not telegram_token:
41
+ self.token = os.getenv('TELEGRAM_TOKEN')
42
+ if not self.token:
43
+ raise ValueError(
44
+ "`TELEGRAM_TOKEN` not found in environment variables. "
45
+ "Get it from t.me/BotFather."
46
+ )
47
+ else:
48
+ self.token = telegram_token
49
+
50
+ try:
51
+ import telebot # type: ignore[import-untyped]
52
+ except ImportError:
53
+ raise ImportError(
54
+ "Please install `telegram_bot` first. "
55
+ "You can install it by running "
56
+ "`pip install pyTelegramBotAPI`."
57
+ )
58
+ self.bot = telebot.TeleBot(token=self.token)
59
+
60
+ # Register the message handler within the constructor
61
+ self.bot.message_handler(func=lambda message: True)(self.on_message)
62
+
63
+ def run(self) -> None:
64
+ r"""Start the Telegram bot."""
65
+ print("Telegram bot is running...")
66
+ self.bot.infinity_polling()
67
+
68
+ def on_message(self, message: 'Message') -> None:
69
+ r"""Handles incoming messages from the user.
70
+
71
+ Args:
72
+ message (types.Message): The incoming message object.
73
+ """
74
+ self.chat_agent.reset()
75
+
76
+ if not message.text:
77
+ return
78
+
79
+ user_msg = BaseMessage.make_user_message(
80
+ role_name="User", content=message.text
81
+ )
82
+ assistant_response = self.chat_agent.step(user_msg)
83
+
84
+ self.bot.reply_to(message, assistant_response.msg.content)
camel/configs/__init__.py CHANGED
@@ -13,6 +13,7 @@
13
13
  # =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
14
14
  from .anthropic_config import ANTHROPIC_API_PARAMS, AnthropicConfig
15
15
  from .base_config import BaseConfig
16
+ from .litellm_config import LITELLM_API_PARAMS, LiteLLMConfig
16
17
  from .openai_config import (
17
18
  OPENAI_API_PARAMS,
18
19
  ChatGPTConfig,
@@ -26,4 +27,6 @@ __all__ = [
26
27
  'AnthropicConfig',
27
28
  'ANTHROPIC_API_PARAMS',
28
29
  'OpenSourceConfig',
30
+ 'LiteLLMConfig',
31
+ 'LITELLM_API_PARAMS',
29
32
  ]
@@ -15,7 +15,7 @@ from __future__ import annotations
15
15
 
16
16
  from dataclasses import asdict, dataclass
17
17
 
18
- from anthropic._types import NOT_GIVEN, NotGiven
18
+ from anthropic import NOT_GIVEN, NotGiven
19
19
 
20
20
  from camel.configs.base_config import BaseConfig
21
21
 
@@ -0,0 +1,113 @@
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 __future__ import annotations
15
+
16
+ from dataclasses import asdict, dataclass, field
17
+ from typing import List, Optional, Union
18
+
19
+ from camel.configs.base_config import BaseConfig
20
+
21
+
22
+ @dataclass(frozen=True)
23
+ class LiteLLMConfig(BaseConfig):
24
+ r"""Defines the parameters for generating chat completions using the
25
+ LiteLLM API.
26
+
27
+ Args:
28
+ model (str): The name of the language model to use for text completion.
29
+ messages (List): A list of message objects representing the
30
+ conversation context. (default: [])
31
+ timeout (Optional[Union[float, str]], optional): Request timeout.
32
+ (default: None)
33
+ temperature (Optional[float], optional): Temperature parameter for
34
+ controlling randomness. (default: None)
35
+ top_p (Optional[float], optional): Top-p parameter for nucleus
36
+ sampling. (default: None)
37
+ n (Optional[int], optional): Number of completions to generate.
38
+ (default: None)
39
+ stream (Optional[bool], optional): Whether to return a streaming
40
+ response. (default: None)
41
+ stream_options (Optional[dict], optional): Options for the streaming
42
+ response. (default: None)
43
+ stop (Optional[Union[str, List[str]]], optional): Sequences where the
44
+ API will stop generating further tokens. (default: None)
45
+ max_tokens (Optional[int], optional): Maximum number of tokens to
46
+ generate. (default: None)
47
+ presence_penalty (Optional[float], optional): Penalize new tokens
48
+ based on their existence in the text so far. (default: None)
49
+ frequency_penalty (Optional[float], optional): Penalize new tokens
50
+ based on their frequency in the text so far. (default: None)
51
+ logit_bias (Optional[dict], optional): Modify the probability of
52
+ specific tokens appearing in the completion. (default: None)
53
+ user (Optional[str], optional): A unique identifier representing the
54
+ end-user. (default: None)
55
+ response_format (Optional[dict], optional): Response format
56
+ parameters. (default: None)
57
+ seed (Optional[int], optional): Random seed. (default: None)
58
+ tools (Optional[List], optional): List of tools. (default: None)
59
+ tool_choice (Optional[Union[str, dict]], optional): Tool choice
60
+ parameters. (default: None)
61
+ logprobs (Optional[bool], optional): Whether to return log
62
+ probabilities of the output tokens. (default: None)
63
+ top_logprobs (Optional[int], optional): Number of most likely tokens
64
+ to return at each token position. (default: None)
65
+ deployment_id (Optional[str], optional): Deployment ID. (default: None)
66
+ extra_headers (Optional[dict], optional): Additional headers for the
67
+ request. (default: None)
68
+ base_url (Optional[str], optional): Base URL for the API. (default:
69
+ None)
70
+ api_version (Optional[str], optional): API version. (default: None)
71
+ api_key (Optional[str], optional): API key. (default: None)
72
+ model_list (Optional[list], optional): List of API base, version,
73
+ keys. (default: None)
74
+ mock_response (Optional[str], optional): Mock completion response for
75
+ testing or debugging. (default: None)
76
+ custom_llm_provider (Optional[str], optional): Non-OpenAI LLM
77
+ provider. (default: None)
78
+ max_retries (Optional[int], optional): Maximum number of retries.
79
+ (default: None)
80
+ """
81
+
82
+ model: str = "gpt-3.5-turbo"
83
+ messages: List = field(default_factory=list)
84
+ timeout: Optional[Union[float, str]] = None
85
+ temperature: Optional[float] = None
86
+ top_p: Optional[float] = None
87
+ n: Optional[int] = None
88
+ stream: Optional[bool] = None
89
+ stream_options: Optional[dict] = None
90
+ stop: Optional[Union[str, List[str]]] = None
91
+ max_tokens: Optional[int] = None
92
+ presence_penalty: Optional[float] = None
93
+ frequency_penalty: Optional[float] = None
94
+ logit_bias: Optional[dict] = field(default_factory=dict)
95
+ user: Optional[str] = None
96
+ response_format: Optional[dict] = None
97
+ seed: Optional[int] = None
98
+ tools: Optional[List] = field(default_factory=list)
99
+ tool_choice: Optional[Union[str, dict]] = None
100
+ logprobs: Optional[bool] = None
101
+ top_logprobs: Optional[int] = None
102
+ deployment_id: Optional[str] = None
103
+ extra_headers: Optional[dict] = field(default_factory=dict)
104
+ base_url: Optional[str] = None
105
+ api_version: Optional[str] = None
106
+ api_key: Optional[str] = None
107
+ model_list: Optional[list] = field(default_factory=list)
108
+ mock_response: Optional[str] = None
109
+ custom_llm_provider: Optional[str] = None
110
+ max_retries: Optional[int] = None
111
+
112
+
113
+ LITELLM_API_PARAMS = {param for param in asdict(LiteLLMConfig()).keys()}
@@ -41,6 +41,19 @@ class ChatGPTConfig(BaseConfig):
41
41
  (default: :obj:`1.0`)
42
42
  n (int, optional): How many chat completion choices to generate for
43
43
  each input message. (default: :obj:`1`)
44
+ response_format (object, optional): An object specifying the format
45
+ that the model must output. Compatible with GPT-4 Turbo and all
46
+ GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Setting to
47
+ {"type": "json_object"} enables JSON mode, which guarantees the
48
+ message the model generates is valid JSON. Important: when using
49
+ JSON mode, you must also instruct the model to produce JSON
50
+ yourself via a system or user message. Without this, the model
51
+ may generate an unending stream of whitespace until the generation
52
+ reaches the token limit, resulting in a long-running and seemingly
53
+ "stuck" request. Also note that the message content may be
54
+ partially cut off if finish_reason="length", which indicates the
55
+ generation exceeded max_tokens or the conversation exceeded the
56
+ max context length.
44
57
  stream (bool, optional): If True, partial message deltas will be sent
45
58
  as data-only server-sent events as they become available.
46
59
  (default: :obj:`False`)
@@ -95,6 +108,7 @@ class ChatGPTConfig(BaseConfig):
95
108
  stop: str | Sequence[str] | NotGiven = NOT_GIVEN
96
109
  max_tokens: int | NotGiven = NOT_GIVEN
97
110
  presence_penalty: float = 0.0
111
+ response_format: dict | NotGiven = NOT_GIVEN
98
112
  frequency_penalty: float = 0.0
99
113
  logit_bias: dict = field(default_factory=dict)
100
114
  user: str = ""
@@ -14,9 +14,11 @@
14
14
  from .base import BaseEmbedding
15
15
  from .openai_embedding import OpenAIEmbedding
16
16
  from .sentence_transformers_embeddings import SentenceTransformerEncoder
17
+ from .vlm_embedding import VisionLanguageEmbedding
17
18
 
18
19
  __all__ = [
19
20
  "BaseEmbedding",
20
21
  "OpenAIEmbedding",
21
22
  "SentenceTransformerEncoder",
23
+ "VisionLanguageEmbedding",
22
24
  ]
@@ -18,7 +18,7 @@ from openai import OpenAI
18
18
 
19
19
  from camel.embeddings.base import BaseEmbedding
20
20
  from camel.types import EmbeddingModelType
21
- from camel.utils import api_key_required
21
+ from camel.utils import model_api_key_required
22
22
 
23
23
 
24
24
  class OpenAIEmbedding(BaseEmbedding[str]):
@@ -46,7 +46,7 @@ class OpenAIEmbedding(BaseEmbedding[str]):
46
46
  self._api_key = api_key or os.environ.get("OPENAI_API_KEY")
47
47
  self.client = OpenAI(timeout=60, max_retries=3, api_key=self._api_key)
48
48
 
49
- @api_key_required
49
+ @model_api_key_required
50
50
  def embed_list(
51
51
  self,
52
52
  objs: List[str],
@@ -11,7 +11,7 @@
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, List, Union
14
+ from typing import Any, List
15
15
 
16
16
  from camel.embeddings.base import BaseEmbedding
17
17
 
@@ -38,17 +38,18 @@ class SentenceTransformerEncoder(BaseEmbedding[str]):
38
38
 
39
39
  def embed_list(
40
40
  self,
41
- objs: Union[str, List[str]],
41
+ objs: List[str],
42
42
  **kwargs: Any,
43
- ) -> list:
43
+ ) -> List[List[float]]:
44
44
  r"""Generates embeddings for the given texts using the model.
45
45
 
46
46
  Args:
47
- objs (str | List[str]): The texts for which to generate the
47
+ objs (List[str]): The texts for which to generate the
48
48
  embeddings.
49
49
 
50
50
  Returns:
51
- list: A list of float representing embeddings.
51
+ List[List[float]]: A list that represents the generated embedding
52
+ as a list of floating-point numbers.
52
53
  """
53
54
  if not objs:
54
55
  raise ValueError("Input text list is empty")
@@ -0,0 +1,146 @@
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, List, Optional, Union
15
+
16
+ from PIL import Image
17
+
18
+ from camel.embeddings import BaseEmbedding
19
+
20
+
21
+ class VisionLanguageEmbedding(BaseEmbedding[Union[str, Image.Image]]):
22
+ r"""Provides image embedding functionalities using multimodal model.
23
+
24
+ Args:
25
+ model_name : The model type to be used for generating embeddings.
26
+ And the default value is: obj:`openai/clip-vit-base-patch32`.
27
+
28
+ Raises:
29
+ RuntimeError: If an unsupported model type is specified.
30
+ """
31
+
32
+ def __init__(
33
+ self, model_name: str = "openai/clip-vit-base-patch32"
34
+ ) -> None:
35
+ r"""Initializes the: obj: `VisionLanguageEmbedding` class with a
36
+ specified model and return the dimension of embeddings.
37
+
38
+ Args:
39
+ model_name (str, optional): The version name of the model to use.
40
+ (default: :obj:`openai/clip-vit-base-patch32`)
41
+ """
42
+ from transformers import AutoModel, AutoProcessor
43
+
44
+ try:
45
+ self.model = AutoModel.from_pretrained(model_name)
46
+ self.processor = AutoProcessor.from_pretrained(model_name)
47
+ except Exception as e:
48
+ raise RuntimeError(f"Failed to load model '{model_name}': {e}")
49
+
50
+ self.valid_processor_kwargs = []
51
+ self.valid_model_kwargs = []
52
+
53
+ try:
54
+ self.valid_processor_kwargs = (
55
+ self.processor.image_processor._valid_processor_keys
56
+ )
57
+ self.valid_model_kwargs = [
58
+ "pixel_values",
59
+ "return_dict",
60
+ "interpolate_pos_encoding",
61
+ ]
62
+ except Exception:
63
+ print("Warning: not typically processor and model structure")
64
+ pass
65
+ self.dim: Optional[int] = None
66
+
67
+ def embed_list(
68
+ self, objs: List[Union[Image.Image, str]], **kwargs: Any
69
+ ) -> List[List[float]]:
70
+ """Generates embeddings for the given images or texts.
71
+
72
+ Args:
73
+ objs (List[Image.Image|str]): The list of images or texts for
74
+ which to generate the embeddings.
75
+ image_processor_kwargs: Extra kwargs passed to the image processor.
76
+ tokenizer_kwargs: Extra kwargs passed to the text tokenizer
77
+ (processor).
78
+ model_kwargs: Extra kwargs passed to the main model.
79
+
80
+ Returns:
81
+ List[List[float]]: A list that represents the generated embedding
82
+ as a list of floating-point numbers.
83
+
84
+ Raises:
85
+ ValueError: If the input type is not `Image.Image` or `str`.
86
+ """
87
+ if not objs:
88
+ raise ValueError("Input objs list is empty.")
89
+
90
+ image_processor_kwargs: Optional[dict] = kwargs.get(
91
+ 'image_processor_kwargs', {}
92
+ )
93
+ tokenizer_kwargs: Optional[dict] = kwargs.get('tokenizer_kwargs', {})
94
+ model_kwargs: Optional[dict] = kwargs.get('model_kwargs', {})
95
+
96
+ result_list = []
97
+ for obj in objs:
98
+ if isinstance(obj, Image.Image):
99
+ image_input = self.processor(
100
+ images=obj,
101
+ return_tensors="pt",
102
+ padding=True,
103
+ **image_processor_kwargs,
104
+ )
105
+ image_feature = (
106
+ self.model.get_image_features(
107
+ **image_input, **model_kwargs
108
+ )
109
+ .squeeze(dim=0)
110
+ .tolist()
111
+ )
112
+ result_list.append(image_feature)
113
+ elif isinstance(obj, str):
114
+ text_input = self.processor(
115
+ text=obj,
116
+ return_tensors="pt",
117
+ padding=True,
118
+ **tokenizer_kwargs,
119
+ )
120
+ text_feature = (
121
+ self.model.get_text_features(**text_input, **model_kwargs)
122
+ .squeeze(dim=0)
123
+ .tolist()
124
+ )
125
+ result_list.append(text_feature)
126
+ else:
127
+ raise ValueError("Input type is not image nor text.")
128
+
129
+ self.dim = len(result_list[0])
130
+
131
+ if any(len(result) != self.dim for result in result_list):
132
+ raise ValueError("Dimensionality is not consistent.")
133
+
134
+ return result_list
135
+
136
+ def get_output_dim(self) -> int:
137
+ r"""Returns the output dimension of the embeddings.
138
+
139
+ Returns:
140
+ int: The dimensionality of the embedding for the current model.
141
+ """
142
+ if self.dim is None:
143
+ text = 'dimension'
144
+ inputs = self.processor(text=[text], return_tensors="pt")
145
+ self.dim = self.model.get_text_features(**inputs).shape[1]
146
+ return self.dim
@@ -17,6 +17,7 @@ from .openai_function import (
17
17
  get_openai_function_schema,
18
18
  get_openai_tool_schema,
19
19
  )
20
+ from .open_api_specs.security_config import openapi_security_config
20
21
 
21
22
  from .google_maps_function import MAP_FUNCS
22
23
  from .math_functions import MATH_FUNCS
@@ -27,10 +28,18 @@ from .twitter_function import TWITTER_FUNCS
27
28
  from .weather_functions import WEATHER_FUNCS
28
29
  from .slack_functions import SLACK_FUNCS
29
30
 
31
+ from .open_api_function import (
32
+ apinames_filepaths_to_funs_schemas,
33
+ generate_apinames_filepaths,
34
+ )
35
+
30
36
  __all__ = [
31
37
  'OpenAIFunction',
32
38
  'get_openai_function_schema',
33
39
  'get_openai_tool_schema',
40
+ 'openapi_security_config',
41
+ 'apinames_filepaths_to_funs_schemas',
42
+ 'generate_apinames_filepaths',
34
43
  'MAP_FUNCS',
35
44
  'MATH_FUNCS',
36
45
  'OPENAPI_FUNCS',