menikev commited on
Commit
403e83f
·
verified ·
1 Parent(s): 279f9a8

Update crypto_analysis_agents.py

Browse files
Files changed (1) hide show
  1. crypto_analysis_agents.py +18 -26
crypto_analysis_agents.py CHANGED
@@ -1,10 +1,9 @@
1
  from crewai import Agent
2
  from langchain.llms import HuggingFacePipeline
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
  from crypto_tools import CryptoTools
5
  from news_tools import NewsTools
6
  from sentiment_tools import SentimentTools
7
- import torch
8
  import os
9
 
10
  class CryptoAnalysisAgents:
@@ -14,29 +13,22 @@ class CryptoAnalysisAgents:
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
15
  model = AutoModelForCausalLM.from_pretrained(model_name)
16
 
17
- # Function to handle generation with manual configuration
18
- def generate_text(input_text, max_new_tokens=512):
19
- input_ids = tokenizer.encode(input_text, return_tensors="pt")
20
- max_length = input_ids.shape[1] + max_new_tokens # Ensure max_length accommodates input + generation
21
-
22
- # Generate the text using the model's generate method
23
- output_ids = model.generate(
24
- input_ids=input_ids,
25
- max_length=max_length, # Set max_length to handle input + generated tokens
26
- temperature=0.7,
27
- top_p=0.95,
28
- top_k=50,
29
- repetition_penalty=1.1,
30
- do_sample=True,
31
- pad_token_id=tokenizer.eos_token_id # Ensure the model doesn't stop too early
32
- )
33
-
34
- # Decode the output ids to get the generated text
35
- output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
36
- return output_text
37
 
38
- # Wrap this in a LangChain pipeline
39
- self.llm = HuggingFacePipeline(generate=generate_text)
40
 
41
  def market_analyst(self):
42
  return Agent(
@@ -90,6 +82,6 @@ if __name__ == "__main__":
90
  # Input text for generation (example)
91
  input_text = "Analyze the current state of Bitcoin in the market."
92
 
93
- # Generate a report
94
- report = market_analyst.llm.generate(input_text)
95
  print(report)
 
1
  from crewai import Agent
2
  from langchain.llms import HuggingFacePipeline
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
  from crypto_tools import CryptoTools
5
  from news_tools import NewsTools
6
  from sentiment_tools import SentimentTools
 
7
  import os
8
 
9
  class CryptoAnalysisAgents:
 
13
  tokenizer = AutoTokenizer.from_pretrained(model_name)
14
  model = AutoModelForCausalLM.from_pretrained(model_name)
15
 
16
+ # Create the text-generation pipeline
17
+ pipe = pipeline(
18
+ "text-generation",
19
+ model=model,
20
+ tokenizer=tokenizer,
21
+ max_new_tokens=512, # Control the maximum number of tokens to generate
22
+ do_sample=True,
23
+ temperature=0.7,
24
+ top_p=0.95,
25
+ top_k=50,
26
+ repetition_penalty=1.1,
27
+ pad_token_id=tokenizer.eos_token_id # Ensure correct padding
28
+ )
 
 
 
 
 
 
 
29
 
30
+ # Wrap the pipeline in a HuggingFacePipeline
31
+ self.llm = HuggingFacePipeline(pipeline=pipe)
32
 
33
  def market_analyst(self):
34
  return Agent(
 
82
  # Input text for generation (example)
83
  input_text = "Analyze the current state of Bitcoin in the market."
84
 
85
+ # Generate a report using the pipeline
86
+ report = market_analyst.llm(input_text)
87
  print(report)