Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: support ollama.com structured-outputs Support ollama structured output. (#1305)

Will Shanged9229789d87
diff --git a/camel/configs/ollama_config.py b/camel/configs/ollama_config.py
index 5139c55ed..cba6e01e6 100644
--- a/camel/configs/ollama_config.py
+++ b/camel/configs/ollama_config.py
@@ -13,7 +13,9 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
from __future__ import annotations
-from typing import Sequence, Union
+from typing import Sequence, Type, Union
+
+from pydantic import BaseModel
from camel.configs.base_config import BaseConfig
from camel.types import NOT_GIVEN, NotGiven
@@ -75,7 +77,7 @@ class OllamaConfig(BaseConfig):
stop: Union[str, Sequence[str], NotGiven] = NOT_GIVEN
max_tokens: Union[int, NotGiven] = NOT_GIVEN
presence_penalty: float = 0.0
- response_format: Union[dict, NotGiven] = NOT_GIVEN
+ response_format: Union[Type[BaseModel], dict, NotGiven] = NOT_GIVEN
frequency_penalty: float = 0.0
diff --git a/camel/models/base_model.py b/camel/models/base_model.py
index bab52e63b..37e9d4061 100644
--- a/camel/models/base_model.py
+++ b/camel/models/base_model.py
@@ -21,6 +21,7 @@ from camel.types import (
ChatCompletion,
ChatCompletionChunk,
ModelType,
+ ParsedChatCompletion,
UnifiedModelType,
)
from camel.utils import BaseTokenCounter
@@ -114,6 +115,33 @@ class BaseModelBackend(ABC):
"""
return self.token_counter.count_tokens_from_messages(messages)
+ def _to_chat_completion(
+ self, response: ParsedChatCompletion
+ ) -> ChatCompletion:
+ if len(response.choices) > 1:
+ print("Warning: Multiple response choices detected")
+
+ choice = dict(
+ index=response.choices[0].index,
+ message={
+ "role": response.choices[0].message.role,
+ "content": response.choices[0].message.content,
+ "tool_calls": response.choices[0].message.tool_calls,
+ "parsed": response.choices[0].message.parsed,
+ },
+ finish_reason=response.choices[0].finish_reason,
+ )
+
+ obj = ChatCompletion.construct(
+ id=response.id,
+ choices=[choice],
+ created=response.created,
+ model=response.model,
+ object="chat.completion",
+ usage=response.usage,
+ )
+ return obj
+
@property
def token_limit(self) -> int:
r"""Returns the maximum token limit for a given model.
diff --git a/camel/models/ollama_model.py b/camel/models/ollama_model.py
index 7c8052e6c..f1d9fb25f 100644
--- a/camel/models/ollama_model.py
+++ b/camel/models/ollama_model.py
@@ -134,6 +134,18 @@ class OllamaModel(BaseModelBackend):
`ChatCompletion` in the non-stream mode, or
`Stream[ChatCompletionChunk]` in the stream mode.
"""
+ if self.model_config_dict.get("response_format"):
+ # stream is not supported in beta.chat.completions.parse
+ if "stream" in self.model_config_dict:
+ del self.model_config_dict["stream"]
+
+ response = self._client.beta.chat.completions.parse(
+ messages=messages,
+ model=self.model_type,
+ **self.model_config_dict,
+ )
+
+ return self._to_chat_completion(response)
response = self._client.chat.completions.create(
messages=messages,
diff --git a/camel/models/openai_model.py b/camel/models/openai_model.py
index 8e8f8b1f1..22cf4c3ef 100644
--- a/camel/models/openai_model.py
+++ b/camel/models/openai_model.py
@@ -24,7 +24,6 @@ from camel.types import (
ChatCompletion,
ChatCompletionChunk,
ModelType,
- ParsedChatCompletion,
)
from camel.utils import (
BaseTokenCounter,
@@ -148,31 +147,6 @@ class OpenAIModel(BaseModelBackend):
)
return response
- def _to_chat_completion(
- self, response: "ParsedChatCompletion"
- ) -> ChatCompletion:
- # TODO: Handle n > 1 or warn consumers it's not supported
- choice = dict(
- index=response.choices[0].index,
- message={
- "role": response.choices[0].message.role,
- "content": response.choices[0].message.content,
- "tool_calls": response.choices[0].message.tool_calls,
- "parsed": response.choices[0].message.parsed,
- },
- finish_reason=response.choices[0].finish_reason,
- )
-
- obj = ChatCompletion.construct(
- id=response.id,
- choices=[choice],
- created=response.created,
- model=response.model,
- object="chat.completion",
- usage=response.usage,
- )
- return obj
-
def check_model_config(self):
r"""Check whether the model configuration contains any
unexpected arguments to OpenAI API.
diff --git a/examples/models/ollama_model_example.py b/examples/models/ollama_model_example.py
index 0633df539..c14479bcd 100644
--- a/examples/models/ollama_model_example.py
+++ b/examples/models/ollama_model_example.py
@@ -12,6 +12,8 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+from pydantic import BaseModel
+
from camel.agents import ChatAgent
from camel.models import ModelFactory
from camel.types import ModelPlatformType
@@ -26,7 +28,7 @@ assistant_sys_msg = "You are a helpful assistant."
agent = ChatAgent(assistant_sys_msg, model=ollama_model, token_limit=4096)
-user_msg = """Say hi to CAMEL AI, one open-source community
+user_msg = """Say hi to CAMEL AI, one open-source community
dedicated to the study of autonomous and communicative agents."""
assistant_response = agent.step(user_msg)
@@ -50,3 +52,65 @@ By the way, what topics would you like to explore within the realm of
autonomous and communicative agents?
===============================================================================
"""
+
+
+class Pet(BaseModel):
+ name: str
+ animal: str
+ age: int
+ color: str | None
+ favorite_toy: str | None
+
+
+class PetList(BaseModel):
+ pets: list[Pet]
+
+
+ollama_model = ModelFactory.create(
+ model_platform=ModelPlatformType.OLLAMA,
+ model_type="llama3.2",
+ # Ensure using ollama verison >= 0.5.1 to use structured output feature
+ model_config_dict={"temperature": 0, "response_format": PetList},
+)
+
+assistant_sys_msg = "You are a helpful assistant."
+
+agent = ChatAgent(assistant_sys_msg, model=ollama_model, token_limit=4096)
+
+user_msg = """I have two pets.A cat named Luna who is 5 years old and loves
+ playing with yarn. She has grey fur. I also have a 2 year old
+ black cat named Loki who loves tennis balls."""
+
+assistant_response = agent.step(user_msg)
+print(assistant_response.msg.content)
+print(assistant_response.msg.parsed)
+
+"""
+===========================================================================
+[{'role': 'system', 'content': 'You are a helpful assistant.'}, {'role':
+'user', 'content': 'I have two pets.A cat named Luna who is 5 years old
+and loves playing with yarn. She has grey fur.I also have a 2 year old
+black cat named Loki who loves tennis balls.'}]
+{ "pets": [
+ {
+ "age": 5,
+ "animal": "cat",
+ "color": "grey",
+ "favorite_toy": "yarn"
+ ,
+ "name": "Luna"
+},
+{
+ "age": 2,
+ "animal": "cat",
+ "color": "black",
+ "favorite_toy": "tennis balls"
+ ,
+ "name": "Loki"
+}]}
+
+pets=[Pet(name='Luna', animal='cat', age=5, color='grey',
+favorite_toy='yarn'), Pet(name='Loki', animal='cat', age=2,
+color='black', favorite_toy='tennis balls')]
+===========================================================================
+"""