Views
No views yet
transformers version 4.55.4 and we advise you to use the latest version of transformers.1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "John1604/John1604"
4
5# load the tokenizer and the model
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype="auto",
10 device_map="auto"
11)
12
13# prepare the model input. If using chinese, use chinese prompt.
14prompt = "Give me a list to web security test tools."
15messages = [
16 {"role": "user", "content": prompt}
17]
18text = tokenizer.apply_chat_template(
19 messages,
20 tokenize=False,
21 add_generation_prompt=True,
22)
23model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
24
25# conduct text completion
26generated_ids = model.generate(
27 **model_inputs,
28 max_new_tokens=16384
29)
30output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
31
32content = tokenizer.decode(output_ids, skip_special_tokens=True)
33
34print("content:", content)1import torch
2from langchain.chains.llm import LLMChain
3from langchain_core.prompts import PromptTemplate
4from langchain_huggingface import HuggingFacePipeline
5from transformers import pipeline
6
7generate_text = pipeline(model="John1604/John1604", torch_dtype=torch.bfloat16,
8 trust_remote_code=True, device_map="auto", return_full_text=True)
9
10prompt = PromptTemplate(
11 input_variables=["instruction"],
12 template="{instruction}")
13
14# template for an instruction with input
15prompt_with_context = PromptTemplate(
16 input_variables=["instruction", "context"],
17 template="{instruction}\n\nInput:\n{context}")
18
19hf_pipeline = HuggingFacePipeline(pipeline=generate_text)
20
21llm_chain = LLMChain(llm=hf_pipeline, prompt=prompt)
22llm_context_chain = LLMChain(llm=hf_pipeline, prompt=prompt_with_context)
23
24print(llm_chain.predict(instruction="List OWASP 10."))
25
26input1 = """George Washington (February 22, 1732[b] - December 14, 1799) was an American military officer, statesman,
27and Founding Father who served as the first president of the United States from 1789 to 1797."""
28
29print(llm_context_chain.predict(instruction="When was George Washington president?", context=input1))