mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: updated declarative samples and handling of non-pydantic response formats (#5022)
* updated declarative samples and handling of non-pydantic response formats * fixed from comments * update docstring
This commit is contained in:
@@ -382,7 +382,10 @@ class OllamaChatClient(
|
||||
except Exception as ex:
|
||||
raise ChatClientException(f"Ollama chat request failed : {ex}", ex) from ex
|
||||
|
||||
return self._parse_response_from_ollama(response)
|
||||
return self._parse_response_from_ollama(
|
||||
response,
|
||||
response_format=validated_options.get("response_format"),
|
||||
)
|
||||
|
||||
return _get_response()
|
||||
|
||||
@@ -536,7 +539,12 @@ class OllamaChatClient(
|
||||
created_at=response.created_at,
|
||||
)
|
||||
|
||||
def _parse_response_from_ollama(self, response: OllamaChatResponse) -> ChatResponse:
|
||||
def _parse_response_from_ollama(
|
||||
self,
|
||||
response: OllamaChatResponse,
|
||||
*,
|
||||
response_format: Any | None = None,
|
||||
) -> ChatResponse:
|
||||
contents = self._parse_contents_from_ollama(response)
|
||||
|
||||
return ChatResponse(
|
||||
@@ -547,6 +555,7 @@ class OllamaChatClient(
|
||||
input_token_count=response.prompt_eval_count,
|
||||
output_token_count=response.eval_count,
|
||||
),
|
||||
response_format=response_format,
|
||||
)
|
||||
|
||||
def _parse_tool_calls_from_ollama(self, tool_calls: Sequence[OllamaMessage.ToolCall]) -> list[Content]:
|
||||
|
||||
@@ -248,6 +248,33 @@ async def test_cmc(
|
||||
assert result.text == "test"
|
||||
|
||||
|
||||
@patch.object(AsyncClient, "chat", new_callable=AsyncMock)
|
||||
async def test_cmc_response_format_dict(
|
||||
mock_chat: AsyncMock,
|
||||
ollama_unit_test_env: dict[str, str],
|
||||
chat_history: list[Message],
|
||||
) -> None:
|
||||
mock_chat.return_value = OllamaChatResponse(
|
||||
message=OllamaMessage(content='{"answer": "test"}', role="assistant"),
|
||||
model="test",
|
||||
eval_count=1,
|
||||
prompt_eval_count=1,
|
||||
created_at="2024-01-01T00:00:00Z",
|
||||
)
|
||||
chat_history.append(Message(text="hello world", role="system"))
|
||||
chat_history.append(Message(text="hello world", role="user"))
|
||||
|
||||
ollama_client = OllamaChatClient()
|
||||
result = await ollama_client.get_response(
|
||||
messages=chat_history,
|
||||
options={"response_format": {"type": "object", "properties": {"answer": {"type": "string"}}}},
|
||||
)
|
||||
|
||||
assert result.value is not None
|
||||
assert isinstance(result.value, dict)
|
||||
assert result.value["answer"] == "test"
|
||||
|
||||
|
||||
@patch.object(AsyncClient, "chat", new_callable=AsyncMock)
|
||||
async def test_cmc_reasoning(
|
||||
mock_chat: AsyncMock,
|
||||
|
||||
Reference in New Issue
Block a user