bwilkie commited on
Commit
a94813f
·
verified ·
1 Parent(s): 394e9ab

Update myagent.py

Browse files
Files changed (1) hide show
  1. myagent.py +38 -22
myagent.py CHANGED
@@ -43,30 +43,46 @@ class BasicAgent:
43
 
44
 
45
 
46
- # model = OpenAIServerModel(
47
- # model_id="gpt-4.1-nano",
48
- # api_base="https://api.openai.com/v1",
49
- # api_key=os.environ["OPENAI_API_KEY"],
50
- # )
51
-
52
-
53
- model_id = "bartowski/Llama-3.2-3B-Instruct-GGUF"
54
- filename = "Llama-3.2-3B-Instruct-Q4_K_M.gguf"
55
-
56
- torch_dtype = torch.float32 # could be torch.float16 or torch.bfloat16 too
57
- tokenizer = AutoTokenizer.from_pretrained(model_id, gguf_file=filename)
58
- model_init = AutoModelForCausalLM.from_pretrained(model_id, gguf_file=filename, torch_dtype=torch_dtype)
59
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- def model(prompt: str, max_new_tokens=512):
62
- input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
63
- output_ids = model_init.generate(input_ids, max_new_tokens=max_new_tokens)
64
- output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
65
- return output
66
 
67
- reviewer_agent= ToolCallingAgent(model=model, tools=[])
68
- model_agent = ToolCallingAgent(model=model,tools=[fetch_webpage])
69
- gaia_agent = CodeAgent(tools=[fetch_webpage,get_youtube_title_description,get_youtube_transcript ], model=model)
 
 
 
 
70
 
71
  if __name__ == "__main__":
72
  # Example usage
 
43
 
44
 
45
 
46
+ # Create a wrapper class that matches the expected interface
47
+ class LocalLlamaModel:
48
+ def __init__(self, model, tokenizer):
49
+ self.model = model
50
+ self.tokenizer = tokenizer
51
+ self.device = model.device if hasattr(model, 'device') else 'cpu'
52
+
53
+ def generate(self, prompt: str, max_new_tokens=512, **kwargs):
54
+ """Generate text using the local model"""
55
+ input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids.to(self.device)
56
+
57
+ with torch.no_grad():
58
+ output_ids = self.model.generate(
59
+ input_ids,
60
+ max_new_tokens=max_new_tokens,
61
+ do_sample=True,
62
+ temperature=0.7,
63
+ pad_token_id=self.tokenizer.eos_token_id,
64
+ **kwargs
65
+ )
66
+
67
+ # Decode only the new tokens (excluding the input)
68
+ new_tokens = output_ids[0][input_ids.shape[1]:]
69
+ output = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
70
+ return output
71
+
72
+ def __call__(self, prompt: str, max_new_tokens=512, **kwargs):
73
+ """Make the model callable like a function"""
74
+ return self.generate(prompt, max_new_tokens, **kwargs)
75
 
76
+ # Create the model instance
77
+ model = LocalLlamaModel(model_init, tokenizer)
 
 
 
78
 
79
+ # Now create your agents - these should work with the wrapped model
80
+ reviewer_agent = ToolCallingAgent(model=model, tools=[])
81
+ model_agent = ToolCallingAgent(model=model, tools=[fetch_webpage])
82
+ gaia_agent = CodeAgent(
83
+ tools=[fetch_webpage, get_youtube_title_description, get_youtube_transcript],
84
+ model=model
85
+ )
86
 
87
  if __name__ == "__main__":
88
  # Example usage