implementing initial LLM class

This commit is contained in:
João Moura
2024-09-22 22:37:29 -03:00
parent 000e2666fb
commit 18c3925fa3
15 changed files with 2217 additions and 47 deletions

View File

@@ -27,7 +27,8 @@ class Converter(OutputConverter):
if self.is_gpt:
return self._create_instructor().to_pydantic()
else:
return LLM(model=self.llm).call(
llm = self._create_llm()
return llm.call(
[
{"role": "system", "content": self.instructions},
{"role": "user", "content": self.text},
@@ -46,8 +47,9 @@ class Converter(OutputConverter):
if self.is_gpt:
return self._create_instructor().to_json()
else:
llm = self._create_llm()
return json.dumps(
LLM(model=self.llm).call(
llm.call(
[
{"role": "system", "content": self.instructions},
{"role": "user", "content": self.text},
@@ -59,6 +61,19 @@ class Converter(OutputConverter):
return self.to_json(current_attempt + 1)
return ConverterError(f"Failed to convert text into JSON, error: {e}.")
def _create_llm(self):
"""Create an LLM instance."""
if isinstance(self.llm, str):
return LLM(model=self.llm)
elif isinstance(self.llm, LLM):
return self.llm
else:
return LLM(
model=self.llm.model,
provider=getattr(self.llm, "provider", "litellm"),
**getattr(self.llm, "llm_kwargs", {}),
)
def _create_instructor(self):
"""Create an instructor."""
from crewai.utilities import InternalInstructor