Final_Assignment_Template / llm_factory.py
24Arys11's picture
embracing langgraph; addapted LLMFactory, Args.LLMInterface, IAgent, Toolbox and agents.py; simplified graph: removed math and encryption agents while promoting the reasoner
4fb4269
raw
history blame
3.88 kB
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
from langchain_openai import ChatOpenAI
from typing import Optional
from args import LLMInterface, Args
class AgentPreset:
def __init__(self, interface: LLMInterface, model_name: str, temperature: Optional[float] = None,
max_tokens: Optional[int] = None, repeat_penalty: Optional[float] = None):
"""
Initialize an AgentPreset with LLM configuration parameters.
Args:
interface: The model interface to use (e.g., OPENAI, HUGGINGFACE)
model_name: Name of the model to use
temperature: Controls randomness in responses (0.0-1.0)
max_tokens: Maximum number of tokens to generate in response
repeat_penalty: Penalty for token repetition
"""
self.interface = interface
self.model_name = model_name
self.temperature = temperature
self.max_tokens = max_tokens
self.repeat_penalty = repeat_penalty
def get_interface(self) -> LLMInterface:
"""
Get the model interface.
Returns:
LLMInterface: The interface used for this agent.
"""
return self.interface
def get_model_name(self) -> str:
"""
Get the model name.
Returns:
str: The name of the model.
"""
return self.model_name
def get_temperature(self) -> float:
"""
Get the temperature setting.
Returns:
float: The temperature value controlling randomness.
"""
return self.temperature
def get_max_tokens(self) -> int:
"""
Get the maximum tokens setting.
Returns:
int: The maximum number of tokens for generation.
"""
return self.max_tokens
def get_repeat_penalty(self) -> float:
"""
Get the repeat penalty setting.
Returns:
float: The penalty value for token repetition.
"""
return self.repeat_penalty
class LLMFactory():
@classmethod
def create(cls, agent_preset: AgentPreset):
interface = agent_preset.get_interface()
if interface == LLMInterface.OPENAI:
model = cls._create_openai_model(agent_preset)
elif interface == LLMInterface.HUGGINGFACE:
model = cls._create_huggingface_model(agent_preset)
else:
raise ValueError(f"Interface '{interface}' is not supported !")
return model
@staticmethod
def _create_openai_model(agent_preset: AgentPreset):
model_name = agent_preset.get_model_name()
temperature = agent_preset.get_temperature()
max_tokens = agent_preset.get_max_tokens()
repeat_penalty = agent_preset.get_repeat_penalty()
kwargs = {
"model": model_name,
"base_url": Args.api_base,
"api_key": Args.api_key,
"temperature": temperature,
"max_completion_tokens": max_tokens,
# "presence_penalty": repeat_penalty,
"frequency_penalty": repeat_penalty
}
model = ChatOpenAI(**kwargs)
return model
@staticmethod
def _create_huggingface_model(agent_preset: AgentPreset):
model_name = agent_preset.get_model_name()
temperature = agent_preset.get_temperature()
max_tokens = agent_preset.get_max_tokens()
repeat_penalty = agent_preset.get_repeat_penalty()
kwargs = {
"model": model_name,
"temperature": temperature,
"max_new_tokens": max_tokens,
"repetition_penalty": repeat_penalty
}
llm = HuggingFaceEndpoint(**kwargs)
model = ChatHuggingFace(llm=llm)
return model