Update myagent.py
Browse files- myagent.py +38 -22
myagent.py
CHANGED
@@ -43,30 +43,46 @@ class BasicAgent:
|
|
43 |
|
44 |
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
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 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
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
|