Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
2
3model_name_or_path = "thesven/openchat-3.6-8b-20240522-GPTQ"
4
5tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
6model = AutoModelForCausalLM.from_pretrained(model_name_or_path,
7 device_map="auto",
8 trust_remote_code=False,
9 revision="main")
10model.pad_token = model.config.eos_token_id
11
12
13prompt_template=f'''
14<<SYS>> You are a very creative story writer. Write a store on the following topic:<</SYS>>
15[INST] Write a story about Ai[/INST]
16[ASSISTANT]
17'''
18
19input_ids = tokenizer(prompt_template, return_tensors='pt').input_ids.cuda()
20output = model.generate(inputs=input_ids, temperature=0.1, do_sample=True, top_p=0.95, top_k=40, max_new_tokens=512)
21
22print(tokenizer.decode(output[0]))

--tensor-parallel-size N to the serving command.localhost:18888 for requests and is compatible with the OpenAI ChatCompletion API specifications. Please refer to the example request below for reference. Additionally, you can use the OpenChat Web UI for a user-friendly experience.--api-keys sk-KEY1 sk-KEY2 ... to specify allowed API keys and --disable-log-requests --disable-log-stats --log-file openchat.log for logging only to a file. For security purposes, we recommend using an HTTPS gateway in front of the server.| Model | Size | Context | Weights | Serving |
|---|---|---|---|---|
| OpenChat-3.6-20240522 | 8B | 8192 | Huggingface | python -m ochat.serving.openai_api_server --model openchat/openchat-3.6-8b-20240522 |
1curl http://localhost:18888/v1/chat/completions \
2 -H "Content-Type: application/json" \
3 -d '{
4 "model": "openchat_3.6",
5 "messages": [{"role": "user", "content": "You are a large language model named OpenChat. Write a poem to describe yourself"}]
6 }'GPT4 Correct User: Hello<|end_of_turn|>GPT4 Correct Assistant: Hi<|end_of_turn|>GPT4 Correct User: How are you today?<|end_of_turn|>GPT4 Correct Assistant:<|end_of_turn|> as end of generation token.tokenizer.chat_template, which can be used instead of manually specifying the template:1messages = [
2 {"role": "user", "content": "Hello"},
3 {"role": "assistant", "content": "Hi"},
4 {"role": "user", "content": "How are you today?"}
5]
6tokens = tokenizer.apply_chat_template(messages, add_generation_prompt=True)1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_id = "openchat/openchat-3.6-8b-20240522"
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
8
9messages = [
10 {"role": "user", "content": "Explain how large language models work in detail."},
11]
12input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
13
14outputs = model.generate(input_ids,
15 do_sample=True,
16 temperature=0.5,
17 max_new_tokens=1024
18)
19response = outputs[0][input_ids.shape[-1]:]
20print(tokenizer.decode(response, skip_special_tokens=True))@article{wang2023openchat,
title={OpenChat: Advancing Open-source Language Models with Mixed-Quality Data},
author={Wang, Guan and Cheng, Sijie and Zhan, Xianyuan and Li, Xiangang and Song, Sen and Liu, Yang},
journal={arXiv preprint arXiv:2309.11235},
year={2023}
}