feat: add variable to control instructor mode
This commit is contained in:
parent
a5bd504daa
commit
2337d36f7b
7 changed files with 58 additions and 7 deletions
|
|
@ -38,6 +38,7 @@ class LLMConfig(BaseSettings):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
structured_output_framework: str = "instructor"
|
structured_output_framework: str = "instructor"
|
||||||
|
llm_instructor_mode: Optional[str] = None
|
||||||
llm_provider: str = "openai"
|
llm_provider: str = "openai"
|
||||||
llm_model: str = "openai/gpt-5-mini"
|
llm_model: str = "openai/gpt-5-mini"
|
||||||
llm_endpoint: str = ""
|
llm_endpoint: str = ""
|
||||||
|
|
@ -181,6 +182,7 @@ class LLMConfig(BaseSettings):
|
||||||
instance.
|
instance.
|
||||||
"""
|
"""
|
||||||
return {
|
return {
|
||||||
|
"llm_instructor_mode": self.llm_instructor_mode,
|
||||||
"provider": self.llm_provider,
|
"provider": self.llm_provider,
|
||||||
"model": self.llm_model,
|
"model": self.llm_model,
|
||||||
"endpoint": self.llm_endpoint,
|
"endpoint": self.llm_endpoint,
|
||||||
|
|
|
||||||
|
|
@ -28,13 +28,19 @@ class AnthropicAdapter(LLMInterface):
|
||||||
|
|
||||||
name = "Anthropic"
|
name = "Anthropic"
|
||||||
model: str
|
model: str
|
||||||
|
default_instructor_mode = "anthropic_tools"
|
||||||
|
|
||||||
def __init__(self, max_completion_tokens: int, model: str = None):
|
def __init__(self, max_completion_tokens: int, model: str = None):
|
||||||
import anthropic
|
import anthropic
|
||||||
|
|
||||||
|
config_instructor_mode = get_llm_config().llm_instructor_mode
|
||||||
|
instructor_mode = (
|
||||||
|
config_instructor_mode if config_instructor_mode else self.default_instructor_mode
|
||||||
|
)
|
||||||
|
|
||||||
self.aclient = instructor.patch(
|
self.aclient = instructor.patch(
|
||||||
create=anthropic.AsyncAnthropic(api_key=get_llm_config().llm_api_key).messages.create,
|
create=anthropic.AsyncAnthropic(api_key=get_llm_config().llm_api_key).messages.create,
|
||||||
mode=instructor.Mode.ANTHROPIC_TOOLS,
|
mode=instructor.Mode(instructor_mode),
|
||||||
)
|
)
|
||||||
|
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ class GeminiAdapter(LLMInterface):
|
||||||
name: str
|
name: str
|
||||||
model: str
|
model: str
|
||||||
api_key: str
|
api_key: str
|
||||||
|
default_instructor_mode = "json_mode"
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -63,7 +64,16 @@ class GeminiAdapter(LLMInterface):
|
||||||
self.fallback_api_key = fallback_api_key
|
self.fallback_api_key = fallback_api_key
|
||||||
self.fallback_endpoint = fallback_endpoint
|
self.fallback_endpoint = fallback_endpoint
|
||||||
|
|
||||||
self.aclient = instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON)
|
from cognee.infrastructure.llm.config import get_llm_config
|
||||||
|
|
||||||
|
config_instructor_mode = get_llm_config().llm_instructor_mode
|
||||||
|
instructor_mode = (
|
||||||
|
config_instructor_mode if config_instructor_mode else self.default_instructor_mode
|
||||||
|
)
|
||||||
|
|
||||||
|
self.aclient = instructor.from_litellm(
|
||||||
|
litellm.acompletion, mode=instructor.Mode(instructor_mode)
|
||||||
|
)
|
||||||
|
|
||||||
@retry(
|
@retry(
|
||||||
stop=stop_after_delay(128),
|
stop=stop_after_delay(128),
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ class GenericAPIAdapter(LLMInterface):
|
||||||
name: str
|
name: str
|
||||||
model: str
|
model: str
|
||||||
api_key: str
|
api_key: str
|
||||||
|
default_instructor_mode = "json_mode"
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -63,7 +64,16 @@ class GenericAPIAdapter(LLMInterface):
|
||||||
self.fallback_api_key = fallback_api_key
|
self.fallback_api_key = fallback_api_key
|
||||||
self.fallback_endpoint = fallback_endpoint
|
self.fallback_endpoint = fallback_endpoint
|
||||||
|
|
||||||
self.aclient = instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON)
|
from cognee.infrastructure.llm.config import get_llm_config
|
||||||
|
|
||||||
|
config_instructor_mode = get_llm_config().llm_instructor_mode
|
||||||
|
instructor_mode = (
|
||||||
|
config_instructor_mode if config_instructor_mode else self.default_instructor_mode
|
||||||
|
)
|
||||||
|
|
||||||
|
self.aclient = instructor.from_litellm(
|
||||||
|
litellm.acompletion, mode=instructor.Mode(instructor_mode)
|
||||||
|
)
|
||||||
|
|
||||||
@retry(
|
@retry(
|
||||||
stop=stop_after_delay(128),
|
stop=stop_after_delay(128),
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ class MistralAdapter(LLMInterface):
|
||||||
model: str
|
model: str
|
||||||
api_key: str
|
api_key: str
|
||||||
max_completion_tokens: int
|
max_completion_tokens: int
|
||||||
|
default_instructor_mode = "mistral_tools"
|
||||||
|
|
||||||
def __init__(self, api_key: str, model: str, max_completion_tokens: int, endpoint: str = None):
|
def __init__(self, api_key: str, model: str, max_completion_tokens: int, endpoint: str = None):
|
||||||
from mistralai import Mistral
|
from mistralai import Mistral
|
||||||
|
|
@ -44,9 +45,14 @@ class MistralAdapter(LLMInterface):
|
||||||
self.model = model
|
self.model = model
|
||||||
self.max_completion_tokens = max_completion_tokens
|
self.max_completion_tokens = max_completion_tokens
|
||||||
|
|
||||||
|
config_instructor_mode = get_llm_config().llm_instructor_mode
|
||||||
|
instructor_mode = (
|
||||||
|
config_instructor_mode if config_instructor_mode else self.default_instructor_mode
|
||||||
|
)
|
||||||
|
|
||||||
self.aclient = instructor.from_litellm(
|
self.aclient = instructor.from_litellm(
|
||||||
litellm.acompletion,
|
litellm.acompletion,
|
||||||
mode=instructor.Mode.MISTRAL_TOOLS,
|
mode=instructor.Mode(instructor_mode),
|
||||||
api_key=get_llm_config().llm_api_key,
|
api_key=get_llm_config().llm_api_key,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,8 @@ class OllamaAPIAdapter(LLMInterface):
|
||||||
- aclient
|
- aclient
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
default_instructor_mode = "json_mode"
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, endpoint: str, api_key: str, model: str, name: str, max_completion_tokens: int
|
self, endpoint: str, api_key: str, model: str, name: str, max_completion_tokens: int
|
||||||
):
|
):
|
||||||
|
|
@ -51,8 +53,16 @@ class OllamaAPIAdapter(LLMInterface):
|
||||||
self.endpoint = endpoint
|
self.endpoint = endpoint
|
||||||
self.max_completion_tokens = max_completion_tokens
|
self.max_completion_tokens = max_completion_tokens
|
||||||
|
|
||||||
|
from cognee.infrastructure.llm.config import get_llm_config
|
||||||
|
|
||||||
|
config_instructor_mode = get_llm_config().llm_instructor_mode
|
||||||
|
instructor_mode = (
|
||||||
|
config_instructor_mode if config_instructor_mode else self.default_instructor_mode
|
||||||
|
)
|
||||||
|
|
||||||
self.aclient = instructor.from_openai(
|
self.aclient = instructor.from_openai(
|
||||||
OpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON
|
OpenAI(base_url=self.endpoint, api_key=self.api_key),
|
||||||
|
mode=instructor.Mode(instructor_mode),
|
||||||
)
|
)
|
||||||
|
|
||||||
@retry(
|
@retry(
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@ class OpenAIAdapter(LLMInterface):
|
||||||
model: str
|
model: str
|
||||||
api_key: str
|
api_key: str
|
||||||
api_version: str
|
api_version: str
|
||||||
|
default_instructor_mode = "json_schema_mode"
|
||||||
|
|
||||||
MAX_RETRIES = 5
|
MAX_RETRIES = 5
|
||||||
|
|
||||||
|
|
@ -74,14 +75,20 @@ class OpenAIAdapter(LLMInterface):
|
||||||
fallback_api_key: str = None,
|
fallback_api_key: str = None,
|
||||||
fallback_endpoint: str = None,
|
fallback_endpoint: str = None,
|
||||||
):
|
):
|
||||||
|
from cognee.infrastructure.llm.config import get_llm_config
|
||||||
|
|
||||||
|
config_instructor_mode = get_llm_config().llm_instructor_mode
|
||||||
|
instructor_mode = (
|
||||||
|
config_instructor_mode if config_instructor_mode else self.default_instructor_mode
|
||||||
|
)
|
||||||
# TODO: With gpt5 series models OpenAI expects JSON_SCHEMA as a mode for structured outputs.
|
# TODO: With gpt5 series models OpenAI expects JSON_SCHEMA as a mode for structured outputs.
|
||||||
# Make sure all new gpt models will work with this mode as well.
|
# Make sure all new gpt models will work with this mode as well.
|
||||||
if "gpt-5" in model:
|
if "gpt-5" in model:
|
||||||
self.aclient = instructor.from_litellm(
|
self.aclient = instructor.from_litellm(
|
||||||
litellm.acompletion, mode=instructor.Mode.JSON_SCHEMA
|
litellm.acompletion, mode=instructor.Mode(instructor_mode)
|
||||||
)
|
)
|
||||||
self.client = instructor.from_litellm(
|
self.client = instructor.from_litellm(
|
||||||
litellm.completion, mode=instructor.Mode.JSON_SCHEMA
|
litellm.completion, mode=instructor.Mode(instructor_mode)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.aclient = instructor.from_litellm(litellm.acompletion)
|
self.aclient = instructor.from_litellm(litellm.acompletion)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue