Adding better support for open source tool calling models (#952)

* Adding better support for open source tool calling models

* making sure the right tool is called

* fixing tests

* better support opensource models
This commit is contained in:
João Moura
2024-07-17 01:54:13 -07:00
committed by GitHub
parent 99ada42d97
commit 7baaeacac3
10 changed files with 5642 additions and 909 deletions

View File

@@ -38,10 +38,10 @@ class Converter(OutputConverter):
return self._create_instructor().to_json()
else:
return json.dumps(self._create_chain().invoke({}).model_dump())
except Exception:
except Exception as e:
if current_attempt < self.max_attempts:
return self.to_json(current_attempt + 1)
return ConverterError("Failed to convert text into JSON.")
return ConverterError(f"Failed to convert text into JSON, error: {e}.")
def _create_instructor(self):
"""Create an instructor."""

View File

@@ -17,6 +17,16 @@ class CrewPydanticOutputParser(PydanticOutputParser):
def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any:
result[0].text = self._transform_in_valid_json(result[0].text)
# Treating edge case of function calling llm returning the name instead of tool_name
json_object = json.loads(result[0].text)
json_object["tool_name"] = (
json_object["name"]
if "tool_name" not in json_object
else json_object["tool_name"]
)
result[0].text = json.dumps(json_object)
json_object = super().parse_result(result)
try:
return self.pydantic_object.parse_obj(json_object)