Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
de0a29a
add user_specified_langchain_llm_factory
Noravee Jun 24, 2025
ecdee78
fix indent and pylint
Noravee Jun 24, 2025
d23186e
Add more comments
Noravee Jun 24, 2025
928ea36
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jun 26, 2025
58b3b8b
remove ollama response error
Noravee Jun 26, 2025
5906c6f
Merge branch 'UN-3276_Support_for_user-defined_llm_in_hocon_file_with…
Noravee Jun 26, 2025
8a8128f
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jun 26, 2025
23891db
minor changes
Noravee Jun 30, 2025
2a8131f
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jul 2, 2025
8e4d881
- Remove user_specified_langchain_llm_factory
Noravee Jul 2, 2025
962abf7
Merge branch 'UN-3276_Support_for_user-defined_llm_in_hocon_file_with…
Noravee Jul 2, 2025
17974b5
use alias for api error in langchain run context
Noravee Jul 2, 2025
9d90aaa
change comments
Noravee Jul 2, 2025
7647dc9
remove space
Noravee Jul 2, 2025
741e50e
Add comments
Noravee Jul 2, 2025
f8ce89d
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jul 2, 2025
69889ad
- Refactor logic on creating llm based on "class" into another method
Noravee Jul 3, 2025
9363936
Merge branch 'UN-3276_Support_for_user-defined_llm_in_hocon_file_with…
Noravee Jul 3, 2025
a796cf8
Add type hints
Noravee Jul 3, 2025
bbc3fbc
Add callbacks
Noravee Jul 3, 2025
50bd7f0
combine user config with the one in class in llm_info
Noravee Jul 4, 2025
1aac685
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jul 7, 2025
69f38f2
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jul 7, 2025
e6dc921
remove optional
Noravee Jul 7, 2025
0b98a39
Merge branch 'UN-3276_Support_for_user-defined_llm_in_hocon_file_with…
Noravee Jul 7, 2025
cca1d03
refactor default llm factory with resolver util
Noravee Jul 8, 2025
b021671
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jul 8, 2025
35c0362
Merge branch 'main' into UN-3276_Support_for_user-defined_llm_in_hoco…
Noravee Jul 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
from logging import Logger
from logging import getLogger

from openai import APIError
from anthropic import BadRequestError
from anthropic import AuthenticationError
import openai
import anthropic

from pydantic_core import ValidationError

Expand Down Expand Up @@ -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:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use APIError for anthropic as well.

message: str = ApiKeyErrorCheck.check_for_api_key_exception(api_error)
if message is not None:
raise ValueError(message) from api_error
Expand All @@ -509,6 +508,11 @@ async def ainvoke(self, agent_executor: AgentExecutor, inputs: Dict[str, Any], i
retries = retries - 1
exception = key_error
backtrace = traceback.format_exc()
except TypeError as type_error:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add TypeError for non-existing arguments user may pass.

self.logger.warning("retrying from TypeError")
retries = retries - 1
exception = type_error
backtrace = traceback.format_exc()
except ValueError as value_error:
response = str(value_error)
find_string = "An output parsing error occurred. " + \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from neuro_san.internals.run_context.langchain.llms.langchain_llm_factory import LangChainLlmFactory
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.llms.user_specified_langchain_llm_factory import \
UserSpecifiedLangChainLlmFactory
from neuro_san.internals.run_context.langchain.util.api_key_error_check import ApiKeyErrorCheck


Expand Down Expand Up @@ -75,6 +77,8 @@ def __init__(self, config: Optional[Dict[str, Any]] = None):
self.llm_factories: List[LangChainLlmFactory] = [
StandardLangChainLlmFactory()
]
self.llm_class: str = None

if config:
self.llm_info_file: str = config.get("agent_llm_info_file")
else:
Expand Down Expand Up @@ -176,6 +180,14 @@ 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.
"""

self.llm_class = config.get("class")
if self.llm_class:
# If config has "class", it is a user-specified llm so return config as is,
# and replace "StandardLangChainLlmFactory" with "UserSpecifiedLangChainLlmFactory".
self.llm_factories[0] = UserSpecifiedLangChainLlmFactory()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If llm_config has class, use UserSpecifiedLangChainLlmFactory.

return config
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If config has class it is a user-specified llm so return config as is.


default_config: Dict[str, Any] = self.llm_infos.get("default_config")
use_config = self.overlayer.overlay(default_config, config)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

# 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_anthropic.chat_models import ChatAnthropic
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI
from langchain_ollama import ChatOllama
from langchain_nvidia_ai_endpoints import ChatNVIDIA
from langchain_core.callbacks.base import BaseCallbackHandler
from langchain_core.language_models.base import BaseLanguageModel
from langchain_openai.chat_models.azure import AzureChatOpenAI
from langchain_openai.chat_models.base import ChatOpenAI

from neuro_san.internals.run_context.langchain.llms.langchain_llm_factory import LangChainLlmFactory


class UserSpecifiedLangChainLlmFactory(LangChainLlmFactory):
"""
A factory for constructing LLMs based on user-specified configurations provided under the "llm_config"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add UserSpecifiedLangChainLlmFactory that uses arguments from given by the user.

section of the agent network HOCON file.

The specific LLM class to instantiate is determined by the "class" field in "llm_config", and all
other keys in the config are passed as arguments to that class's constructor.
"""

def create_base_chat_model(self, config: Dict[str, Any],
callbacks: List[BaseCallbackHandler] = None) -> BaseLanguageModel:
"""
Create a BaseLanguageModel from the user-specified llm config.
:param config: The user-specified llm config
: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()

# Take "class" out of config and add "callback".
config.pop("class")
config["callbacks"] = callbacks

# Unpack config in the user-specified class
if chat_class == "openai":
llm = ChatOpenAI(**config)
elif chat_class == "azure-openai":
llm = AzureChatOpenAI(**config)
elif chat_class == "anthropic":
llm = ChatAnthropic(**config)
elif chat_class == "ollama":
llm = ChatOllama(**config)
elif chat_class == "nvidia":
llm = ChatNVIDIA(**config)
elif chat_class == "gemini":
llm = ChatGoogleGenerativeAI(**config)
else:
valid_class_map = {
"openai": "ChatOpenAI",
"azure-openai": "AzureChatOpenAI",
"anthropic": "ChatAnthropic",
"ollama": "ChatOllama",
"nvidia": "ChatNVIDIA",
"gemini": "ChatGoogleGenerativeAI",
}
available = "\n".join(f" - '{key}': {val}" for key, val in valid_class_map.items())
raise ValueError(
f"Unrecognized model class '{chat_class}'.\n"
f"Valid class values and their corresponding implementations are:\n{available}"
)

return llm
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@

# Azure OpenAI requires several parameters; all can be set via environment variables
# except "deployment_name", which must be provided explicitly.
"AZURE_OPENAI_API_KEY": ["Error code: 401", "invalid subscription key", "wrong API endpoint", "Connection error"],
"AZURE_OPENAI_ENDPOINT": ["validation error", "base_url", "azure_endpoint", "AZURE_OPENAI_ENDPOINT",
"Connection error"],
"OPENAI_API_VERSION": ["validation error", "api_version", "OPENAI_API_VERSION", "Error code: 404",
"Resource not found"],
"deployment_name": ["Error code: 404", "Resource not found", "API deployment for this resource does not exist"],
"AZURE_OPENAI_API_KEY": ["invalid subscription key", "wrong API endpoint"],
"AZURE_OPENAI_ENDPOINT": ["base_url", "azure_endpoint", "AZURE_OPENAI_ENDPOINT"],
"OPENAI_API_VERSION": ["api_version", "OPENAI_API_VERSION"],
"deployment_name": ["API deployment for this resource does not exist"],
}


Expand Down
4 changes: 2 additions & 2 deletions neuro_san/registries/google_serper.hocon
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
{
"name": "searcher",
"instructions": "Use your tool to respond to the inquiry.",
"function": {
"function": {
# The description acts as an initial prompt.
"description": "Assist user with answer from internet."
}
}
"tools": ["search_tool"]
},
{
Expand Down