-
Notifications
You must be signed in to change notification settings - Fork 28
UN-3276 support for user defined llm in hocon file with class key #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 21 commits
de0a29a
ecdee78
d23186e
928ea36
58b3b8b
5906c6f
8a8128f
23891db
2a8131f
8e4d881
962abf7
17974b5
9d90aaa
7647dc9
741e50e
f8ce89d
69889ad
9363936
a796cf8
bbc3fbc
50bd7f0
1aac685
69f38f2
e6dc921
0b98a39
cca1d03
b021671
35c0362
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
|
|
||
| # Copyright (C) 2023-2025 Cognizant Digital Business, Evolutionary AI. | ||
| # All Rights Reserved. | ||
| # Issued under the Academic Public License. | ||
| # | ||
| # You can be released from the terms, and requirements of the Academic Public | ||
| # License by purchasing a commercial license. | ||
| # Purchase of a commercial license is mandatory for any use of the | ||
| # neuro-san SDK Software in commercial settings. | ||
| # | ||
| # END COPYRIGHT | ||
|
|
||
| from typing import Any | ||
| from typing import Dict | ||
| from typing import List | ||
|
|
||
| from langchain_core.callbacks.base import BaseCallbackHandler | ||
| from langchain_core.language_models.base import BaseLanguageModel | ||
| from langchain_groq import ChatGroq | ||
|
|
||
| from neuro_san.internals.run_context.langchain.llms.langchain_llm_factory import LangChainLlmFactory | ||
|
|
||
|
|
||
| class GroqLangChainLlmFactory(LangChainLlmFactory): | ||
| """ | ||
| Factory class for LLM operations | ||
| """ | ||
|
|
||
| def create_base_chat_model(self, config: Dict[str, Any], | ||
| callbacks: List[BaseCallbackHandler] = None) -> BaseLanguageModel: | ||
| """ | ||
| Create a BaseLanguageModel from the fully-specified llm config. | ||
| :param config: The fully specified llm config which is a product of | ||
| _create_full_llm_config() above. | ||
| :param callbacks: A list of BaseCallbackHandlers to add to the chat model. | ||
| :return: A BaseLanguageModel (can be Chat or LLM) | ||
| Can raise a ValueError if the config's class or model_name value is | ||
| unknown to this method. | ||
| """ | ||
| # Construct the LLM | ||
| llm: BaseLanguageModel = None | ||
| chat_class: str = config.get("class") | ||
| if chat_class is not None: | ||
| chat_class = chat_class.lower() | ||
|
|
||
| model_name: str = config.get("model_name") | ||
|
|
||
| if chat_class == "groq": | ||
| llm = ChatGroq( | ||
| model=model_name, | ||
| temperature=config.get("temperature") | ||
| ) | ||
| elif chat_class is None: | ||
| raise ValueError(f"Class name {chat_class} for model_name {model_name} is unspecified.") | ||
| else: | ||
| raise ValueError(f"Class {chat_class} for model_name {model_name} is unrecognized.") | ||
|
|
||
| return llm | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Copyright (C) 2023-2025 Cognizant Digital Business, Evolutionary AI. | ||
| # All Rights Reserved. | ||
| # Issued under the Academic Public License. | ||
| # | ||
| # You can be released from the terms, and requirements of the Academic Public | ||
| # License by purchasing a commercial license. | ||
| # Purchase of a commercial license is mandatory for any use of the | ||
| # neuro-san SDK Software in commercial settings. | ||
| # | ||
| # END COPYRIGHT | ||
|
|
||
| # The schema specifications for this file are documented here: | ||
| # https://github.com/cognizant-ai-lab/neuro-san/blob/main/docs/llm_info_hocon_reference.md | ||
|
|
||
| { | ||
|
|
||
|
|
||
| "classes": { | ||
| "factories": [ "llm_extension.groq_langchain_llm_factory.GroqLangChainLlmFactory" ] | ||
| "groq": { | ||
|
||
| # Add arguments like temperature that you want to pass to the llm here. | ||
| "temperature": 0.7 | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,8 @@ | |
| from logging import Logger | ||
| from logging import getLogger | ||
|
|
||
| from openai import APIError | ||
| from anthropic import BadRequestError | ||
| from anthropic import AuthenticationError | ||
| from openai import APIError as OpenAI_APIError | ||
| from anthropic import APIError as Anthropic_APIError | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only import |
||
|
|
||
| from pydantic_core import ValidationError | ||
|
|
||
|
|
@@ -495,7 +494,7 @@ async def ainvoke(self, agent_executor: AgentExecutor, inputs: Dict[str, Any], i | |
| while return_dict is None and retries > 0: | ||
| try: | ||
| return_dict: Dict[str, Any] = await agent_executor.ainvoke(inputs, invoke_config) | ||
| except (APIError, BadRequestError, AuthenticationError, ChatGoogleGenerativeAIError) as api_error: | ||
| except (OpenAI_APIError, Anthropic_APIError, ChatGoogleGenerativeAIError) as api_error: | ||
Noravee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| message: str = ApiKeyErrorCheck.check_for_api_key_exception(api_error) | ||
| if message is not None: | ||
| raise ValueError(message) from api_error | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| from neuro_san.internals.run_context.langchain.llms.llm_info_restorer import LlmInfoRestorer | ||
| from neuro_san.internals.run_context.langchain.llms.standard_langchain_llm_factory import StandardLangChainLlmFactory | ||
| from neuro_san.internals.run_context.langchain.util.api_key_error_check import ApiKeyErrorCheck | ||
| from neuro_san.internals.run_context.langchain.util.argument_validator import ArgumentValidator | ||
|
|
||
|
|
||
| class DefaultLlmFactory(ContextTypeLlmFactory, LangChainLlmFactory): | ||
|
|
@@ -123,24 +124,13 @@ def resolve_one_llm_factory(self, llm_factory_class_name: str, llm_info_file: st | |
| raise ValueError(f"The value for the classes.factories key in {llm_info_file} " | ||
| "must be a list of strings") | ||
|
|
||
| class_split: List[str] = llm_factory_class_name.split(".") | ||
| if len(class_split) <= 2: | ||
| raise ValueError(f"Value in the classes.factories in {llm_info_file} must be of the form " | ||
| "<package_name>.<module_name>.<ClassName>") | ||
|
|
||
| # Create a list of a single package given the name in the value | ||
| packages: List[str] = [".".join(class_split[:-2])] | ||
| class_name: str = class_split[-1] | ||
| resolver = Resolver(packages) | ||
|
|
||
| # Resolve the class name | ||
| llm_factory_class: Type[LangChainLlmFactory] = None | ||
| try: | ||
| llm_factory_class: Type[LangChainLlmFactory] = \ | ||
| resolver.resolve_class_in_module(class_name, module_name=class_split[-2]) | ||
| except AttributeError as exception: | ||
| raise ValueError(f"Class {llm_factory_class_name} in {llm_info_file} " | ||
| "not found in PYTHONPATH") from exception | ||
| # Resolve the factory class | ||
| llm_factory_class: Type[LangChainLlmFactory] = self._resolve_class_from_path( | ||
| class_path=llm_factory_class_name, | ||
| expected_base=LangChainLlmFactory, | ||
| source_file=llm_info_file, | ||
| description="classes.factories" | ||
| ) | ||
|
|
||
| # Instantiate it | ||
| try: | ||
|
|
@@ -155,7 +145,43 @@ def resolve_one_llm_factory(self, llm_factory_class_name: str, llm_info_file: st | |
| "must be of type LangChainLlmFactory") | ||
| return llm_factory | ||
|
|
||
| def create_llm(self, config: Dict[str, Any], callbacks: List[BaseCallbackHandler] = None) -> BaseLanguageModel: | ||
| def _resolve_class_from_path( | ||
| self, | ||
| class_path: str, | ||
| expected_base: Type, | ||
| source_file: str, | ||
| description: str | ||
| ) -> Type: | ||
|
||
|
|
||
| parts: List[str] = class_path.split(".") | ||
| if len(parts) <= 2: | ||
| raise ValueError( | ||
| f"Value for '{description}' in {source_file} must be of the form " | ||
| "<package>.<module>.<ClassName>" | ||
| ) | ||
Noravee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| module_name: str = parts[-2] | ||
| class_name: str = parts[-1] | ||
| packages: str = [".".join(parts[:-2])] | ||
| resolver = Resolver(packages) | ||
|
|
||
| try: | ||
| cls: Type = resolver.resolve_class_in_module(class_name, module_name=module_name) | ||
| except AttributeError as e: | ||
| raise ValueError(f"Class {class_path} in {source_file} not found in PYTHONPATH") from e | ||
|
|
||
| if not issubclass(cls, expected_base): | ||
| raise ValueError( | ||
| f"Class {class_path} in {source_file} must be a subclass of {expected_base.__name__}" | ||
| ) | ||
|
|
||
| return cls | ||
|
|
||
| def create_llm( | ||
| self, | ||
| config: Dict[str, Any], | ||
| callbacks: Optional[List[BaseCallbackHandler]] = None | ||
| ) -> BaseLanguageModel: | ||
| """ | ||
| Creates a langchain LLM based on the 'model_name' value of | ||
| the config passed in. | ||
|
|
@@ -176,6 +202,28 @@ def create_full_llm_config(self, config: Dict[str, Any]) -> Dict[str, Any]: | |
| :param config: The llm_config from the user | ||
| :return: The fully specified config with defaults filled in. | ||
| """ | ||
|
|
||
| class_from_llm_config: str = config.get("class") | ||
| if class_from_llm_config: | ||
| if not isinstance(class_from_llm_config, str): | ||
| raise ValueError("Value of 'class' has to be string.") | ||
| # A "class" key in the config indicates the user has specified a particular LLM implementation. | ||
| # However, the config may only contain partial arguments (e.g., {"arg_1": 0.5}) and omit others. | ||
| # | ||
| # In the standard factory, LLM classes are instantiated like: | ||
| # ChatOpenAI(arg_1=config.get("arg_1"), arg_2=config.get("arg_2")) | ||
| # If a required argument like "arg_2" is missing in the config, config.get("arg_2") returns None, | ||
| # which may raise an error during instantiation if the argument has no default. | ||
| # | ||
| # To prevent this, we first fetch the default arguments for the given class from llm_info, | ||
| # then merge them with the user-provided config. This ensures all expected arguments are present, | ||
| # and the user’s config values take precedence over the defaults. | ||
| config_from_class_in_llm_info: Dict[str, Any] = self.get_chat_class_args(class_from_llm_config) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine user-specified config with the one in |
||
|
|
||
| # Merge the defaults from llm_info with the user-defined config, | ||
| # giving priority to values in config. | ||
| return self.overlayer.overlay(config_from_class_in_llm_info, config) | ||
|
|
||
| default_config: Dict[str, Any] = self.llm_infos.get("default_config") | ||
| use_config = self.overlayer.overlay(default_config, config) | ||
|
|
||
|
|
@@ -215,7 +263,7 @@ def create_full_llm_config(self, config: Dict[str, Any]) -> Dict[str, Any]: | |
|
|
||
| return full_config | ||
|
|
||
| def get_chat_class_args(self, chat_class_name: str, use_model_name: str) -> Dict[str, Any]: | ||
| def get_chat_class_args(self, chat_class_name: str, use_model_name: Optional[str] = None) -> Dict[str, Any]: | ||
Noravee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| :param chat_class_name: string name of the chat class to look up. | ||
| :param use_model_name: the original model name that prompted the chat class lookups | ||
|
|
@@ -227,8 +275,9 @@ def get_chat_class_args(self, chat_class_name: str, use_model_name: str) -> Dict | |
| chat_classes: Dict[str, Any] = self.llm_infos.get("classes") | ||
| chat_class: Dict[str, Any] = chat_classes.get(chat_class_name) | ||
| if chat_class is None: | ||
| raise ValueError(f"llm info entry for {use_model_name} uses a 'class' of {chat_class_name} " | ||
| "which is not defined in the 'classes' table.") | ||
| # If chat_class_name is not in "classes" in llm_info, | ||
| # it could be a user-specified langchain model class | ||
| return {} | ||
Noravee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Get the args from the chat class | ||
| args: Dict[str, Any] = chat_class.get("args") | ||
|
|
@@ -242,9 +291,10 @@ def get_chat_class_args(self, chat_class_name: str, use_model_name: str) -> Dict | |
| return args | ||
|
|
||
| def create_base_chat_model(self, config: Dict[str, Any], | ||
| callbacks: List[BaseCallbackHandler] = None) -> BaseLanguageModel: | ||
| callbacks: Optional[List[BaseCallbackHandler]] = None) -> BaseLanguageModel: | ||
Noravee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Create a BaseLanguageModel from the fully-specified llm config. | ||
| Create a BaseLanguageModel from the fully-specified llm config either from standard LLM factory, | ||
| user-defined LLM factory, or user-specified langchain model class. | ||
| :param config: The fully specified llm config which is a product of | ||
| _create_full_llm_config() above. | ||
| :param callbacks: A list of BaseCallbackHandlers to add to the chat model. | ||
|
|
@@ -279,11 +329,55 @@ def create_base_chat_model(self, config: Dict[str, Any], | |
| # Let the next model have a crack | ||
| found_exception = exception | ||
|
|
||
| # Try resolving via 'class' in config if factories failed | ||
| class_path: str = config.get("class") | ||
| if llm is None and found_exception is not None and class_path: | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The conditions to instantiate using
|
||
| llm = self.create_base_chat_model_from_user_class(class_path, config) | ||
| found_exception = None | ||
|
|
||
| if found_exception is not None: | ||
| raise found_exception | ||
|
|
||
| return llm | ||
|
|
||
| def create_base_chat_model_from_user_class( | ||
| self, | ||
| class_path: str, | ||
| config: Dict[str, Any], | ||
| callbacks: Optional[List[BaseCallbackHandler]] = None | ||
| ) -> BaseLanguageModel: | ||
| """ | ||
| Create a BaseLanguageModel from the user-specified langchain model class. | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor the logic of instantiating using "class" and user-defined args to |
||
| :param class_path: A string in the form of <package>.<module>.<Class> | ||
| :param config: The fully specified llm config which is a product of | ||
| _create_full_llm_config() above. | ||
| :param callbacks: A list of BaseCallbackHandlers to add to the chat model. | ||
|
|
||
| :return: A BaseLanguageModel | ||
| """ | ||
|
|
||
| if not isinstance(class_path, str): | ||
| raise ValueError("'class' in llm_config must be a string") | ||
|
|
||
| # Resolve the 'class' | ||
| llm_class: Type[BaseLanguageModel] = self._resolve_class_from_path( | ||
| class_path=class_path, | ||
| expected_base=BaseLanguageModel, | ||
| source_file="agent network hocon file", | ||
| description="llm_config" | ||
| ) | ||
|
|
||
| # Copy the config, take 'class' out, and add callbacks | ||
| # Then unpack into llm constructor | ||
| user_config: Dict[str, Any] = config.copy() | ||
| user_config.pop("class") | ||
| user_config["callbacks"] = callbacks | ||
|
|
||
| # Check for invalid args and throw error if found | ||
| ArgumentValidator.check_invalid_args(llm_class, user_config) | ||
|
|
||
| return llm_class(**user_config) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLM instantiation with args. |
||
|
|
||
| def get_max_prompt_tokens(self, config: Dict[str, Any]) -> int: | ||
| """ | ||
| :param config: A dictionary which describes which LLM to use. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for testing only. It will not be in the final PR.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the spirit of robust tests + limited library dependencies, you could put this class and extra hocon file under the test/ directory and the extra langchain_groq dependency in requirements-build.txt as long as your intent is to add some kind of regularly run test that uses it (unit test if < 15 secs, integration test if longer)