wt002 commited on
Commit
37ebfbe
·
verified ·
1 Parent(s): 19a679b

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +25 -9
agent.py CHANGED
@@ -54,7 +54,7 @@ from langchain.chains import LLMChain
54
  from langchain.agents import initialize_agent, Tool, AgentType
55
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
56
  from huggingface_hub import login
57
-
58
 
59
  load_dotenv()
60
 
@@ -461,16 +461,33 @@ tools = [wiki_tool, calc_tool, file_tool, web_tool, arvix_tool, youtube_tool, vi
461
 
462
 
463
  # Get the Hugging Face API token from the environment variable
 
464
  hf_token = os.getenv("HF_TOKEN")
465
-
466
  login(token=hf_token)
467
 
468
- # Initialize the desired model and parameters
469
  model_name = "mistralai/Mistral-7B-Instruct-v0.1"
 
 
470
  tokenizer = AutoTokenizer.from_pretrained(model_name)
471
- model = AutoModelForCausalLM.from_pretrained(model_name)
472
 
473
- # Create a text generation pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
  pipe = pipeline(
475
  "text-generation",
476
  model=model,
@@ -481,12 +498,10 @@ pipe = pipeline(
481
  repetition_penalty=1.15
482
  )
483
 
484
- # Create LangChain LLM wrapper
485
  llm = HuggingFacePipeline(pipeline=pipe)
486
 
487
-
488
-
489
- # Initialize the LangChain agent with the tool(s) and the model
490
  agent = initialize_agent(
491
  tools=tools,
492
  llm=llm,
@@ -496,6 +511,7 @@ agent = initialize_agent(
496
 
497
 
498
 
 
499
  # -------------------------------
500
  # Step 8: Use the Planner, Classifier, and Decision Logic
501
  # -------------------------------
 
54
  from langchain.agents import initialize_agent, Tool, AgentType
55
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
56
  from huggingface_hub import login
57
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig
58
 
59
  load_dotenv()
60
 
 
461
 
462
 
463
  # Get the Hugging Face API token from the environment variable
464
+ # Load Hugging Face token
465
  hf_token = os.getenv("HF_TOKEN")
 
466
  login(token=hf_token)
467
 
468
+ # Model name
469
  model_name = "mistralai/Mistral-7B-Instruct-v0.1"
470
+
471
+ # Load tokenizer
472
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
473
 
474
+ # 🔧 Use 4-bit or 8-bit quantization
475
+ bnb_config = BitsAndBytesConfig(
476
+ load_in_4bit=True, # or use load_in_8bit=True
477
+ bnb_4bit_compute_dtype="float16",
478
+ bnb_4bit_use_double_quant=True,
479
+ bnb_4bit_quant_type="nf4",
480
+ )
481
+
482
+ # Load model with quantization
483
+ model = AutoModelForCausalLM.from_pretrained(
484
+ model_name,
485
+ device_map="auto",
486
+ quantization_config=bnb_config,
487
+ token=hf_token
488
+ )
489
+
490
+ # Create pipeline
491
  pipe = pipeline(
492
  "text-generation",
493
  model=model,
 
498
  repetition_penalty=1.15
499
  )
500
 
501
+ # LangChain wrapper
502
  llm = HuggingFacePipeline(pipeline=pipe)
503
 
504
+ # Initialize LangChain agent
 
 
505
  agent = initialize_agent(
506
  tools=tools,
507
  llm=llm,
 
511
 
512
 
513
 
514
+
515
  # -------------------------------
516
  # Step 8: Use the Planner, Classifier, and Decision Logic
517
  # -------------------------------