File size: 1,353 Bytes
362de84 |
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 |
"""LLM loading and initialization."""
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from langchain_community.llms import HuggingFacePipeline
import streamlit as st
@st.cache_resource
def load_qwen_llm(model_name, max_new_tokens=256, temperature=0.7, top_p=0.95):
"""
Load Qwen LLM model.
Args:
model_name: HuggingFace model identifier
max_new_tokens: Maximum tokens to generate
temperature: Sampling temperature
top_p: Nucleus sampling parameter
Returns:
HuggingFacePipeline: Wrapped LLM for LangChain
"""
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(
model_name,
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto" if torch.cuda.is_available() else None,
trust_remote_code=True
)
# Create pipeline
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
do_sample=True,
return_full_text=False
)
return HuggingFacePipeline(pipeline=pipe)
|