mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-22 14:49:29 +00:00
(Ollama) Fix String Value parsing in _parse_arguments_from_tool_call (#30154)
- **Description:** Fix String Value parsing in _parse_arguments_from_tool_call - **Issue:** #30145 --------- Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
parent
c0ffc9aa29
commit
1103bdfaf1
@ -125,13 +125,17 @@ def _parse_arguments_from_tool_call(
|
|||||||
if "function" not in raw_tool_call:
|
if "function" not in raw_tool_call:
|
||||||
return None
|
return None
|
||||||
arguments = raw_tool_call["function"]["arguments"]
|
arguments = raw_tool_call["function"]["arguments"]
|
||||||
parsed_arguments = {}
|
parsed_arguments: dict = {}
|
||||||
if isinstance(arguments, dict):
|
if isinstance(arguments, dict):
|
||||||
for key, value in arguments.items():
|
for key, value in arguments.items():
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
parsed_arguments[key] = _parse_json_string(
|
parsed_value = _parse_json_string(
|
||||||
value, skip=True, raw_tool_call=raw_tool_call
|
value, skip=True, raw_tool_call=raw_tool_call
|
||||||
)
|
)
|
||||||
|
if isinstance(parsed_value, (dict, list)):
|
||||||
|
parsed_arguments[key] = parsed_value
|
||||||
|
else:
|
||||||
|
parsed_arguments[key] = value
|
||||||
else:
|
else:
|
||||||
parsed_arguments[key] = value
|
parsed_arguments[key] = value
|
||||||
else:
|
else:
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
"""Test chat model integration."""
|
"""Test chat model integration."""
|
||||||
|
import json
|
||||||
from typing import Dict, Type
|
from typing import Dict, Type
|
||||||
|
|
||||||
from langchain_tests.unit_tests import ChatModelUnitTests
|
from langchain_tests.unit_tests import ChatModelUnitTests
|
||||||
|
|
||||||
from langchain_ollama.chat_models import ChatOllama
|
from langchain_ollama.chat_models import ChatOllama, _parse_arguments_from_tool_call
|
||||||
|
|
||||||
|
|
||||||
class TestChatOllama(ChatModelUnitTests):
|
class TestChatOllama(ChatModelUnitTests):
|
||||||
@ -15,3 +15,11 @@ class TestChatOllama(ChatModelUnitTests):
|
|||||||
@property
|
@property
|
||||||
def chat_model_params(self) -> Dict:
|
def chat_model_params(self) -> Dict:
|
||||||
return {"model": "llama3-groq-tool-use"}
|
return {"model": "llama3-groq-tool-use"}
|
||||||
|
|
||||||
|
|
||||||
|
def test__parse_arguments_from_tool_call() -> None:
|
||||||
|
raw_response = '{"model":"sample-model","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_profile_details","arguments":{"arg_1":"12345678901234567890123456"}}}]},"done":false}' # noqa: E501
|
||||||
|
raw_tool_calls = json.loads(raw_response)["message"]["tool_calls"]
|
||||||
|
response = _parse_arguments_from_tool_call(raw_tool_calls[0])
|
||||||
|
assert response is not None
|
||||||
|
assert isinstance(response["arg_1"], str)
|
||||||
|
Loading…
Reference in New Issue
Block a user