commit
1e52055d65
28
llm/LLM.py
28
llm/LLM.py
|
@ -2,39 +2,39 @@ from llm.Qwen import Qwen
|
||||||
from llm.Gemini import Gemini
|
from llm.Gemini import Gemini
|
||||||
from llm.ChatGPT import ChatGPT
|
from llm.ChatGPT import ChatGPT
|
||||||
from llm.VllmGPT import VllmGPT
|
from llm.VllmGPT import VllmGPT
|
||||||
|
|
||||||
def test_Qwen(question = "如何应对压力?", mode='offline', model_path="Qwen/Qwen-1_8B-Chat"):
|
def test_Qwen(question = "如何应对压力?", mode='offline', model_path="Qwen/Qwen-1_8B-Chat"):
|
||||||
llm = Qwen(mode, model_path)
|
llm = Qwen(mode, model_path)
|
||||||
answer = llm.generate(question)
|
answer = llm.generate(question)
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
def test_Gemini(question = "如何应对压力?", model_path='gemini-pro', api_key=None, proxy_url=None):
|
def test_Gemini(question = "如何应对压力?", model_path='gemini-pro', api_key=None, proxy_url=None):
|
||||||
llm = Gemini(model_path, api_key, proxy_url)
|
llm = Gemini(model_path, api_key, proxy_url)
|
||||||
answer = llm.generate(question)
|
answer = llm.generate(question)
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
class LLM:
|
class LLM:
|
||||||
def __init__(self, mode='offline'):
|
def __init__(self, mode='offline'):
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
|
|
||||||
def init_model(self, model_name, model_path, api_key=None, proxy_url=None):
|
def init_model(self, model_name, model_path, api_key=None, proxy_url=None):
|
||||||
if model_name not in ['Qwen', 'Gemini', 'ChatGPT', 'VllmGPT']:
|
if model_name not in ['Qwen', 'Gemini', 'ChatGPT', 'VllmGPT']:
|
||||||
raise ValueError("model_name must be 'ChatGPT', 'VllmGPT', 'Qwen', or 'Gemini'(其他模型还未集成)")
|
raise ValueError("model_name must be 'ChatGPT', 'VllmGPT', 'Qwen', or 'Gemini'(其他模型还未集成)")
|
||||||
|
|
||||||
if model_name == 'Gemini':
|
if model_name == 'Gemini':
|
||||||
llm = Gemini(model_path, api_key, proxy_url)
|
llm = Gemini(model_path, api_key, proxy_url)
|
||||||
elif model_name == 'ChatGPT':
|
elif model_name == 'ChatGPT':
|
||||||
llm = ChatGPT(model_path, api_key=api_key)
|
llm = ChatGPT(model_path, api_key=api_key)
|
||||||
elif model_name == 'Qwen':
|
elif model_name == 'Qwen':
|
||||||
llm = Qwen(self.mode, model_path)
|
llm = Qwen(model_path=model_path, api_key=api_key, api_base=proxy_url)
|
||||||
elif model_name == 'VllmGPT':
|
elif model_name == 'VllmGPT':
|
||||||
llm = VllmGPT()
|
llm = VllmGPT()
|
||||||
return llm
|
return llm
|
||||||
|
|
||||||
|
|
||||||
def test_Qwen(self, question="如何应对压力?", model_path="Qwen/Qwen-1_8B-Chat"):
|
def test_Qwen(self, question="如何应对压力?", model_path="Qwen/Qwen-1_8B-Chat", api_key=None, proxy_url=None):
|
||||||
llm = Qwen(self.mode, model_path)
|
llm = Qwen(model_path=model_path, api_key=api_key, api_base=proxy_url)
|
||||||
answer = llm.generate(question)
|
answer = llm.chat(question)
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
def test_Gemini(self, question="如何应对压力?", model_path='gemini-pro', api_key=None, proxy_url=None):
|
def test_Gemini(self, question="如何应对压力?", model_path='gemini-pro', api_key=None, proxy_url=None):
|
||||||
|
@ -43,10 +43,12 @@ class LLM:
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# llm = LLM()
|
llm = LLM()
|
||||||
# llm.test_Gemini(api_key='你的API Key', proxy_url=None)
|
# llm.test_Gemini(api_key='你的API Key', proxy_url=None)
|
||||||
# llm = LLM().init_model('Gemini', model_path= 'gemini-pro',api_key='AIzaSyBWAWfT8zsyAZcRIXLS5Vzlw8KKCN9qsAg', proxy_url='http://172.31.71.58:7890')
|
# llm = LLM().init_model('Gemini', model_path= 'gemini-pro',api_key='AIzaSyBWAWfT8zsyAZcRIXLS5Vzlw8KKCN9qsAg', proxy_url='http://172.31.71.58:7890')
|
||||||
# response = llm.chat("如何应对压力?")
|
# response = llm.chat("如何应对压力?")
|
||||||
llm = LLM().init_model('VllmGPT', model_path= 'THUDM/chatglm3-6b')
|
# llm = LLM().init_model('VllmGPT', model_path= 'THUDM/chatglm3-6b')
|
||||||
response = llm.chat("如何应对压力?")
|
# response = llm.chat("如何应对压力?")
|
||||||
# print(response)
|
# print(response)
|
||||||
|
|
||||||
|
llm.test_Qwen(api_key="none", proxy_url="http://10.1.1.113:18000/v1")
|
||||||
|
|
60
llm/Qwen.py
60
llm/Qwen.py
|
@ -1,25 +1,56 @@
|
||||||
import os
|
import os
|
||||||
import torch
|
import openai
|
||||||
import requests
|
|
||||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
'''
|
||||||
|
`huggingface`连接不上可以使用 `modelscope`
|
||||||
|
`pip install modelscope`
|
||||||
|
'''
|
||||||
|
from modelscope import AutoModelForCausalLM, AutoTokenizer
|
||||||
|
#from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||||
|
|
||||||
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
||||||
|
|
||||||
|
|
||||||
class Qwen:
|
class Qwen:
|
||||||
def __init__(self, model_path="Qwen/Qwen-1_8B-Chat") -> None:
|
def __init__(self, model_path="Qwen/Qwen-1_8B-Chat", api_base=None, api_key=None) -> None:
|
||||||
'''暂时不写api版本,与Linly-api相类似,感兴趣可以实现一下'''
|
'''暂时不写api版本,与Linly-api相类似,感兴趣可以实现一下'''
|
||||||
|
# 默认本地推理
|
||||||
|
self.local = True
|
||||||
|
|
||||||
|
# api_base和api_key不为空时使用openapi的方式
|
||||||
|
if api_key is not None and api_base is not None:
|
||||||
|
openai.api_base = api_base
|
||||||
|
openai.api_key = api_key
|
||||||
|
self.local = False
|
||||||
|
return
|
||||||
|
|
||||||
self.model, self.tokenizer = self.init_model(model_path)
|
self.model, self.tokenizer = self.init_model(model_path)
|
||||||
|
self.data = {}
|
||||||
def init_model(self, path = "Qwen/Qwen-1_8B-Chat"):
|
|
||||||
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-1_8B-Chat",
|
def init_model(self, path="Qwen/Qwen-1_8B-Chat"):
|
||||||
device_map="auto",
|
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-1_8B-Chat",
|
||||||
|
device_map="auto",
|
||||||
trust_remote_code=True).eval()
|
trust_remote_code=True).eval()
|
||||||
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
|
||||||
|
|
||||||
return model, tokenizer
|
return model, tokenizer
|
||||||
|
|
||||||
def chat(self, question):
|
|
||||||
|
|
||||||
self.data["question"] = f"{self.prompt} ### Instruction:{question} ### Response:"
|
def chat(self, question):
|
||||||
|
# 优先调用qwen openapi的方式
|
||||||
|
if not self.local:
|
||||||
|
# 不使用流式回复的请求
|
||||||
|
response = openai.ChatCompletion.create(
|
||||||
|
model="Qwen",
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": question}
|
||||||
|
],
|
||||||
|
stream=False,
|
||||||
|
stop=[]
|
||||||
|
)
|
||||||
|
return response.choices[0].message.content
|
||||||
|
|
||||||
|
# 默认本地推理
|
||||||
|
self.data["question"] = f"{question} ### Instruction:{question} ### Response:"
|
||||||
try:
|
try:
|
||||||
response, history = self.model.chat(self.tokenizer, self.data["question"], history=None)
|
response, history = self.model.chat(self.tokenizer, self.data["question"], history=None)
|
||||||
print(history)
|
print(history)
|
||||||
|
@ -27,11 +58,12 @@ class Qwen:
|
||||||
except:
|
except:
|
||||||
return "对不起,你的请求出错了,请再次尝试。\nSorry, your request has encountered an error. Please try again.\n"
|
return "对不起,你的请求出错了,请再次尝试。\nSorry, your request has encountered an error. Please try again.\n"
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
llm = Qwen(model_path="Qwen/Qwen-1_8B-Chat")
|
llm = Qwen(model_path="Qwen/Qwen-1_8B-Chat")
|
||||||
answer = llm.generate("如何应对压力?")
|
answer = llm.chat(question="如何应对压力?")
|
||||||
print(answer)
|
print(answer)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
test()
|
test()
|
||||||
|
|
Loading…
Reference in New Issue