You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
HI im pretty new to python but I'm attempting to follow the tutorial for adding an open source model for usage. I have the following file and I believe I'm registering the model before attempting to use however I'm getting the error below:
The model:
# custom client with custom model loaderfromtransformersimportAutoModelForCausalLM, AutoTokenizer, GenerationConfig, AutoModelForSequenceClassificationimportrandomfromtypesimportSimpleNamespaceclassBertModelClient:
def__init__(self, config, **kwargs):
print(f"CustomModelClient config: {config}")
self.device=config.get("device", "cpu")
self.model=AutoModelForCausalLM.from_pretrained(config["model"]).to(self.device)
self.model_name=config["model"]
self.tokenizer=AutoTokenizer.from_pretrained(config["model"], use_fast=False)
self.tokenizer.pad_token_id=self.tokenizer.eos_token_idself.label_mapping= {
0: "Algebra",
1: "Counting & Probability",
2: "Geometry",
3: "Intermediate Algebra",
4: "Number Theory",
5: "Prealgebra",
6: "Precalculus"
}
# params are set by the user and consumed by the user since they are providing a custom model# so anything can be done heregen_config_params=config.get("params", {})
self.max_length=gen_config_params.get("max_length", 256)
print(f"Loaded model {config['model']} to {self.device}")
defcreate(self, params):
ifparams.get("stream", False) and"messages"inparams:
raiseNotImplementedError("Local models do not support streaming.")
else:
num_of_responses=params.get("n", 1)
text=params.get("text","What is 5+5?")
response=SimpleNamespace()
# inputs = self.tokenizer.apply_chat_template(# params["messages"], return_tensors="pt", add_generation_prompt=True# ).to(self.device)inputs=self.tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
inputs= {key: val.to(self.device) forkey, valininputs.items()}
inputs_length=inputs.shape[-1]
# add inputs_length to max_lengthmax_length=self.max_length+inputs_lengthgeneration_config=GenerationConfig(
max_length=max_length,
eos_token_id=self.tokenizer.eos_token_id,
pad_token_id=self.tokenizer.eos_token_id,
)
response.choices= []
response.model=self.model_namewithtorch.no_grad():
outputs=self.model(**inputs)
predictions=torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class=torch.argmax(predictions, dim=-1).item()
choice=SimpleNamespace()
choice.message=SimpleNamespace()
choice.message.content=predicted_classchoice.message.function_call=Noneresponse.choices.append(choice)
returnresponsefor_inrange(num_of_responses):
outputs=self.model.generate(inputs, generation_config=generation_config)
# Decode only the newly generated text, excluding the prompttext=self.tokenizer.decode(outputs[0, inputs_length:])
choice=SimpleNamespace()
choice.message=SimpleNamespace()
choice.message.content=textchoice.message.function_call=Noneresponse.choices.append(choice)
returnresponsedefmessage_retrieval(self, response):
"""Retrieve the messages from the response."""choices=response.choicesreturn [choice.message.contentforchoiceinchoices]
defcost(self, response) ->float:
"""Calculate the cost of the response."""response.cost=0return0defpredictMathType(self, text):
try:
inputs=self.tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
inputs= {key: val.to(self.device) forkey, valininputs.items()}
withtorch.no_grad():
outputs=self.model(**inputs)
predictions=torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class=torch.argmax(predictions, dim=-1).item()
returnself.label_mapping[predicted_class]
exceptExceptionase:
predicted_class=random.randint(0,6)
returnself.label_mapping[predicted_class]
@staticmethoddefget_usage(response):
# returns a dict of prompt_tokens, completion_tokens, total_tokens, cost, model# if usage needs to be tracked, else Nonereturn {}
importautogen# passes in questionsuser_proxy=autogen.UserProxyAgent(
name="User_proxy",
system_message="A human admin.",
code_execution_config=False,
human_input_mode="TERMINATE",
is_termination_msg=lambdax: x.get("content", "") andx.get("content", "").rstrip().endswith("TERMINATE"),
)
# bert -> classifies the question typeclassifier=autogen.AssistantAgent(
name="Classifier",
system_message="""You an advanced AI acting as the an mathematics procedural assistant; you will take a given math problem written in LaTex format and make a guess at the type of math problem. """,
llm_config=llm_config,
description="""This is a math problem procedure assistant who is capable of taking a problem in LaTex format; and suggesting the appropriate category. """
)
procedure=autogen.AssistantAgent(
name="Procedure",
system_message="""You an advanced AI acting as the an mathematics procedural assistant; you will take a given math problem written in LaTex format, create a high fidelity summary of what the problem is asking you to achieve; then break the task down into a list of individual steps that must be completed to solve the task. """,
llm_config=llm_config,
description="""This is a math problem procedure assistant who is capable of taking a problem in LaTex format; summarizing the question; and creating a procedure for solving it. The procedure assistant is open to any comments and recommendations for improving the summary and procedure. Ask the procedure assistant to iterate every time when there is a new change recommendation from editor. """
)
# deepseek vs opencode for solutioningsolver=autogen.AssistantAgent(
name="Solver",
system_message="""You an advanced AI acting as the an mathematics procedural assistant; you will take a given math problem written in LaTex format and make a guess at the type of math problem. """,
llm_config=llm_config,
description="""This agent will solve the actual math problems. """
)
groupchat=autogen.GroupChat(agents=[user_proxy, classifier, solver], messages=[], max_round=6)
manager=autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
manager.register_model_client(model_client_cls=BertModelClient)
user_proxy.initiate_chat(manager, message="What is 5 + 5")
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
HI im pretty new to python but I'm attempting to follow the tutorial for adding an open source model for usage. I have the following file and I believe I'm registering the model before attempting to use however I'm getting the error below:
The model:
Autogen config:
Chat Setup
The error:
Any help is much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions