-
Notifications
You must be signed in to change notification settings - Fork 0
/
llama2_cli.py
executable file
·87 lines (78 loc) · 3.17 KB
/
llama2_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
###############################################################################
# ** Large Language Models (LLM) user interface**
#
# **Author:** Darrell O. Ricke, Ph.D. (mailto: [email protected])
# Copyright: Copyright (c) 2024 Massachusetts Institute of Technology
# License: GNU GPL license (http://www.gnu.org/licenses/gpl.html)
#
# **RAMS request ID 1026697**
#
# **Overview:**
# Large Language Models (LLM) user interface.
#
# **Citation:** None
#
# **Disclaimer:**
# DISTRIBUTION STATEMENT A. Approved for public release. Distribution is unlimited.
#
# This material is based upon work supported by the Department of the Air Force
# under Air Force Contract No. FA8702-15-D-0001. Any opinions, findings,
# conclusions or recommendations expressed in this material are those of the
# author(s) and do not necessarily reflect the views of the Department of the Air Force.
#
# © 2024 Massachusetts Institute of Technology
#
# The software/firmware is provided to you on an As-Is basis
#
# Delivered to the U.S. Government with Unlimited Rights, as defined in DFARS
# Part 252.227-7013 or 7014 (Feb 2014). Notwithstanding any copyright notice,
# U.S. Government rights in this work are defined by DFARS 252.227-7013 or
# DFARS 252.227-7014 as detailed above. Use of this work other than as specifically
# authorized by the U.S. Government may violate any copyrights that exist in this work.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
###############################################################################
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
import transformers
import torch
import os
import sys
from os import environ
model_id = "meta-llama/Llama-2-7b-chat-hf"
# environ["TRANSFORMERS_OFFLINE"] = "1"
# environ["TRANSFORMERS_CACHE"] = "/raid/LLM/Models/falcon/projects"
#os.environ["CUDA_VISIBLE_DEVICES"]="1,2" # if you need to specify GPUs
hf_token = "hf_..."
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token, load_in_16bit=True, trust_remote_code=True, device_map="auto", )
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
tokenizer=tokenizer,
torch_dtype=torch.bfloat16,
trust_remote_code=True,
device_map="auto",
)
###############################################################################
def call_llm(message):
sequences = pipeline(
message,
max_length=1000,
do_sample=True,
top_k=10,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
)
for seq in sequences:
return seq['generated_text']
###############################################################################
arg_count = len(sys.argv)
if ( arg_count >= 1 ):
print( call_llm(sys.argv[1]) )