Spaces:
Sleeping
Sleeping
File size: 3,879 Bytes
4fb4269 cc6bd3b d26c7f3 4fb4269 d26c7f3 4fb4269 cc6bd3b 4fb4269 d26c7f3 cc6bd3b 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 d26c7f3 4fb4269 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
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
|