├── README.md ├── assets ├── coder-readme.gif ├── demo1.gif ├── demo2.gif ├── star.gif └── yicoder.gif ├── cookbook ├── Fine_tune │ └── finetune.md ├── NL2SQL │ └── NL2SQL.md ├── Quantization │ └── quantization.md ├── System_prompt │ └── System_prompt.ipynb ├── Webpage │ ├── Webpage.md │ ├── app.py │ └── index.html └── test └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 |
2 | yicoder 3 |
4 | 5 |
6 | 🐙 GitHub • 7 | 👾 Discord • 8 | 🐤 Twitter • 9 | 💬 WeChat • 10 | 📑 Blog 11 |
12 | 13 | --- 14 | 15 | - [Intro](#intro) 16 | - [News](#news) 17 | - [Quick Start](#quick-start) 18 | - [Cookbook](#cookbook) 19 | - [Results](#results) 20 | - [License](#license) 21 | 22 | 23 | # Intro 24 | Yi-Coder is a series of open-source code language models that delivers state-of-the-art coding performance with fewer than 10 billion parameters. 25 | 26 | Key features: 27 | - Excelling in long-context understanding with a maximum context length of 128K tokens. 28 | - Supporting 52 major programming languages, including popular ones such as Java, Python, JavaScript, and C++. 29 | 30 | ```bash 31 | 'java', 'markdown', 'python', 'php', 'javascript', 'c++', 'c#', 'c', 'typescript', 'html', 'go', 'java_server_pages', 'dart', 'objective-c', 'kotlin', 'tex', 'swift', 'ruby', 'sql', 'rust', 'css', 'yaml', 'matlab', 'lua', 'json', 'shell', 'visual_basic', 'scala', 'rmarkdown', 'pascal', 'fortran', 'haskell', 'assembly', 'perl', 'julia', 'cmake', 'groovy', 'ocaml', 'powershell', 'elixir', 'clojure', 'makefile', 'coffeescript', 'erlang', 'lisp', 'toml', 'batchfile', 'cobol', 'dockerfile', 'r', 'prolog', 'verilog' 32 | ``` 33 | 34 | | Name | Type | Length | Download | 35 | |--------------------|------|----------------|---------------------------------------------------------------------------------------------------------------------------------------------------| 36 | | Yi-Coder-9B-Chat | Chat | 128K | [🤗 Hugging Face](https://huggingface.co/01-ai/Yi-Coder-9B-Chat) • [🤖 ModelScope](https://www.modelscope.cn/models/01ai/Yi-Coder-9B-Chat) • [🟣 wisemodel](https://wisemodel.cn/models/01.AI/Yi-Coder-9B-Chat) | 37 | | Yi-Coder-1.5B-Chat | Chat | 128K | [🤗 Hugging Face](https://huggingface.co/01-ai/Yi-Coder-1.5B-Chat) • [🤖 ModelScope](https://www.modelscope.cn/models/01ai/Yi-Coder-1.5B-Chat) • [🟣 wisemodel](https://wisemodel.cn/models/01.AI/Yi-Coder-1.5B-Chat) | 38 | | Yi-Coder-9B | Base | 128K | [🤗 Hugging Face](https://huggingface.co/01-ai/Yi-Coder-9B) • [🤖 ModelScope](https://www.modelscope.cn/models/01ai/Yi-Coder-9B) • [🟣 wisemodel](https://wisemodel.cn/models/01.AI/Yi-Coder-9B) | 39 | | Yi-Coder-1.5B | Base | 128K | [🤗 Hugging Face](https://huggingface.co/01-ai/Yi-Coder-1.5B) • [🤖 ModelScope](https://www.modelscope.cn/models/01ai/Yi-Coder-1.5B) • [🟣 wisemodel](https://wisemodel.cn/models/01.AI/Yi-Coder-1.5B) | 40 | 41 | 42 | For more details, see [Yi-Coder blog](https://01-ai.github.io/blog.html?post=en/2024-09-05-A-Small-but-Mighty-LLM-for-Code.md) 43 | 44 | 45 | # News 46 | 🔥 **2024-09-05**: The Yi-Coder series models are open sourced and available to the public. 47 | 48 | # Quick Start 49 | ## Requirements 50 | Make sure you have `python>=3.9` installed before using it. To set up the environment and install the requirements, run the following command: 51 | ```bash 52 | git clone https://github.com/01-ai/Yi-Coder.git 53 | cd Yi-Coder 54 | pip install -r requirements.txt 55 | ``` 56 | ## Ollama 57 | You can run Yi-Coder on Ollama locally. 58 | 59 | 1. After [installing Ollama](https://ollama.com/), you can start the Ollama service. Note that keep this service running while you use Ollama. 60 | 61 | ```python 62 | ollama serve 63 | ``` 64 | 65 | 2. Run Yi-Coder models. For more Yi models supported by Ollama, see [Yi tags](https://ollama.com/library/yi/tags). 66 | 67 | ```python 68 | ollama run yi-coder 69 | ``` 70 | 71 | ## Transformers 72 | You can use transformers to run inference with Yi-Coder models (both chat and base versions) as follows: 73 | ```python 74 | from transformers import AutoTokenizer, AutoModelForCausalLM 75 | 76 | device = "cuda" # the device to load the model onto 77 | model_path = "01-ai/Yi-Coder-9B-Chat" 78 | 79 | tokenizer = AutoTokenizer.from_pretrained(model_path) 80 | model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto").eval() 81 | 82 | prompt = "Write a quick sort algorithm." 83 | messages = [ 84 | {"role": "system", "content": "You are a helpful assistant."}, 85 | {"role": "user", "content": prompt} 86 | ] 87 | text = tokenizer.apply_chat_template( 88 | messages, 89 | tokenize=False, 90 | add_generation_prompt=True 91 | ) 92 | model_inputs = tokenizer([text], return_tensors="pt").to(device) 93 | 94 | generated_ids = model.generate( 95 | model_inputs.input_ids, 96 | max_new_tokens=1024, 97 | eos_token_id=tokenizer.eos_token_id 98 | ) 99 | generated_ids = [ 100 | output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) 101 | ] 102 | 103 | response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] 104 | print(response) 105 | ``` 106 | ## vLLM 107 | You can also use vLLM to reason about Yi-Coder models. vLLM is a fast and easy-to-use library for reasoning about and serving large language models (LLMs). Be sure to install vLLM and then do the following 108 | ```python 109 | from transformers import AutoTokenizer 110 | from vllm import LLM, SamplingParams 111 | model_path = "01-ai/Yi-Coder-9B-Chat" 112 | 113 | tokenizer = AutoTokenizer.from_pretrained(model_path) 114 | 115 | sampling_params = SamplingParams( 116 | temperature=0.8, 117 | top_p=0.8) 118 | 119 | llm = LLM(model=model_path, 120 | gpu_memory_utilization=0.9, 121 | max_model_len=1024) 122 | 123 | prompt = "Write a quick sort algorithm." 124 | messages = [ 125 | {"role": "user", "content": prompt} 126 | ] 127 | text = tokenizer.apply_chat_template( 128 | messages, 129 | tokenize=False, 130 | add_generation_prompt=True 131 | ) 132 | print(text) 133 | 134 | # Generate the response 135 | outputs = llm.generate([text], sampling_params) 136 | 137 | # Print the output 138 | for output in outputs: 139 | prompt = output.prompt 140 | generated_text = output.outputs[0].text 141 | print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}") 142 | 143 | ``` 144 | 145 | # Cookbook 146 | 147 | - [System prompt](https://github.com/01-ai/Yi-Coder/blob/main/cookbook/System_prompt/System_prompt.ipynb): Enhance coding workflow with code completion, insertion, and quality assurance. 148 | - [Webpage](https://github.com/01-ai/Yi-Coder/blob/main/cookbook/Webpage/Webpage.md): Turn your ideas into web pages! 149 | - [NL2SQL](https://github.com/01-ai/Yi-Coder/blob/main/cookbook/NL2SQL/NL2SQL.md): Convert natural language queries into Structured Query Language (SQL). 150 | - [Fine-tune](https://github.com/01-ai/Yi-Coder/blob/main/cookbook/Fine_tune/finetune.md): Fine-tune the Yi-Coder series models for your specific needs. 151 | - [Quantization](https://github.com/01-ai/Yi-Coder/blob/main/cookbook/Quantization/quantization.md): Quantize your Yi-Coder series models using Swift. 152 | 153 | # Results 154 | 155 | For the highlight of full results, please refer to our [blog post](https://01-ai.github.io/blog.html?post=en/2024-09-05-A-Small-but-Mighty-LLM-for-Code.md). 156 | Below we present more detailed results for multilingual HumanEval, CodeEditorBench, and math programming. 157 | 158 | ## Multi-lingual HumanEval 159 | 1) Base model comparison 160 | 161 | | Model | Size | Python | C++ | Java | PHP | TS | C# | Bash | JS | Avg | 162 | |---------------------------|-------|--------|-------|-------|-------|-------|-------|-------|-------|-------| 163 | | CodeLLama | 34B | 48.2 | 44.7 | 44.9 | 41.0 | 42.1 | 48.7 | 15.8 | 42.2 | 41.0 | 164 | | StarCoder2 | 15B | 46.3 | 41.4 | 33.9 | 39.5 | 43.8 | 29.2 | 18.9 | 44.2 | 37.2 | 165 | | DeepSeek-Coder-Base | 1.3B | 34.8 | 31.1 | 32.3 | 24.2 | 28.9 | 36.7 | 10.1 | 28.6 | 28.3 | 166 | | DeepSeek-Coder-Base | 6.7B | 49.4 | 50.3 | 43.0 | 38.5 | 49.7 | 50.0 | 28.5 | 48.4 | 44.7 | 167 | | DeepSeek-Coder-Base | 33B | **56.1** | 58.4 | **51.9** | 44.1 | 52.8 | 51.3 | 32.3 | **55.3** | **50.3** | 168 | | CodeQwen1.5 | 7B | 52.4 | 52.2 | 42.4 | **46.6** | 52.2 | 55.7 | **36.7** | 49.7 | 48.5 | 169 | | Yi-Coder | 1.5B | 41.5 | 37.9 | 32.9 | 34.2 | 35.9 | 38.6 | 8.9 | 39.1 | 33.6 | 170 | | Yi-Coder | 9B | 53.7 | **59.6** | 36.7 | 45.3 | **57.9** | **58.2** | 30.4 | 54.7 | 49.6 | 171 | 172 | 2) Instruction-tuned model comparison 173 | 174 | | Model | Size | Python | C++ | Java | PHP | TS | C# | Bash | JS | Avg | 175 | |---------------------------|-------|--------|-------|-------|-------|-------|-------|-------|-------|-------| 176 | | GPT-4 | | 84.1 | 76.4 | 81.6 | 77.2 | 77.4 | 79.1 | 58.2 | 78.0 | 76.5 | 177 | | DeepSeek-Coder-Instruct | 1.3B | 65.2 | 45.3 | 51.1 | 45.3 | 59.7 | 55.1 | 12.7 | 52.2 | 48.4 | 178 | | DeepSeek-Coder-Instruct | 6.7B | 78.9 | 63.4 | 68.4 | 68.9 | 67.2 | 72.8 | 36.7 | 72.7 | 66.1 | 179 | | DeepSeek-Coder-Instruct | 33B | 79.3 | 68.9 | 73.4 | 72.7 | 67.9 | 74.1 | 43.0 | 73.9 | 69.2 | 180 | | CodeQwen1.5-Chat | 7B | 83.2 | **71.2** | 70.1 | **73.5** | **75.4** | 75.9 | 41.1 | 78.2 | 71.1 | 181 | | Yi-Coder-Chat | 1.5B | 67.7 | 49.1 | 51.9 | 52.2 | 57.9 | 57.6 | 19.0 | 59.6 | 51.9 | 182 | | Yi-Coder-Chat | 9B | **85.4** | 67.7 | **76.0** | 72.1 | 72.3 | **76.6** | **45.6** | **78.9** | **71.8** | 183 | 184 | ## CodeEditorBench 185 | 1) Primary 186 | 187 | | Models | Debug | Translation | Switch | Polish | Avg Win Rate | 188 | | ---------------------- | ------ | ----------- | ------ | ------ | ------------ | 189 | | GPT-4 | 49.30% | 50.30% | 26.40% | 1.33% | 85.72% | 190 | | Yi-Coder-9B-Chat | 46.09% | 42.60% | 14.06% | 7.04% | 78.57% | 191 | | DS-Coder-33B-Instruct | 48.70% | 45.10% | 16.20% | 1.14% | 67.86% | 192 | | CodeQwen1.5-7B-Chat | 42.37% | 34.20% | 11.37% | 5.03% | 57.14% | 193 | | GLM-4 | 27.10% | 36.50% | 8.50% | 6.46% | 50.00% | 194 | | CodeLLaMA-13B-Instruct | 36.80% | 27.50% | 2.10% | 1.82% | 39.29% | 195 | | CodeLLaMA-7b-Instruct | 33.60% | 23.10% | 1.70% | 1.17% | 21.43% | 196 | 197 | 2) Plus 198 | 199 | | Models | Debug | Translation | Switch | Polish | Avg Win Rate | 200 | | ---------------------- | ------ | ----------- | ------ | ------ | ------------ | 201 | | GPT-4 | 31.60% | 46.50% | 26.40% | 1.12% | 82.14% | 202 | | Yi-Coder-9B-Chat | 26.44% | 34.91% | 14.06% | 5.87% | 75.00% | 203 | | DS-Coder-33B-Instruct | 27.50% | 41.00% | 16.20% | 1.10% | 67.86% | 204 | | CodeQwen1.5-7B-Chat | 20.90% | 36.27% | 11.37% | 2.91% | 60.72% | 205 | | GLM-4 | 22.00% | 27.80% | 8.50% | 5.17% | 50.00% | 206 | | CodeLLaMA-13B-Instruct | 17.60% | 33.30% | 2.10% | 2.31% | 39.29% | 207 | | CodeLLaMA-7b-Instruct | 15.50% | 28.90% | 1.70% | 1.47% | 25.00% | 208 | ## Math Programming 209 | | Model | Size | GSM8k | MATH | GSM-Hard | SVAMP | TabMWP | ASDiv | MAWPS | Avg | 210 | | ------------------- | ---- | ----- | ---- | -------- | ----- | ------ | ----- | ----- | ---- | 211 | | CodeShell | 7B | 15.8 | 8.6 | 17.3 | 35.5 | 28.2 | 44.4 | 59.8 | 29.9 | 212 | | CodeGeex-2 | 7B | 22.2 | 9.7 | 23.6 | 39.0 | 44.6 | 48.5 | 66.0 | 36.2 | 213 | | StarCoder-Base | 16B | 23.4 | 10.3 | 23.0 | 42.4 | 45.0 | 54.9 | 81.1 | 40.0 | 214 | | CodeLLama-Base | 7B | 31.2 | 12.1 | 30.2 | 54.2 | 52.9 | 59.6 | 82.6 | 46.1 | 215 | | CodeLLama-Base | 13B | 43.1 | 14.4 | 40.2 | 59.2 | 60.3 | 63.6 | 85.3 | 52.3 | 216 | | CodeLLama-Base | 34B | 58.2 | 21.2 | 51.8 | 70.3 | 69.8 | 70.7 | 91.8 | 62.0 | 217 | | DeepSeek-Coder-Base | 1.3B | 14.6 | 16.8 | 14.5 | 36.7 | 30.0 | 48.2 | 62.3 | 31.9 | 218 | | DeepSeek-Coder-Base | 6.7B | 43.2 | 19.2 | 40.3 | 58.4 | 67.9 | 67.2 | 87.0 | 54.7 | 219 | | DeepSeek-Coder-Base | 33B | 60.7 | 29.1 | 54.1 | 71.6 | 75.3 | 76.7 | 93.3 | 65.8 | 220 | | Yi-Coder | 1.5B | 25.5 | 11.4 | 27.7 | 42.4 | 36.2 | 59.3 | 75.5 | 39.7 | 221 | | Yi-Coder | 9B | **68.1** | **29.1** | **61.0** | **77.8** | **81.2** | **81.0** | **93.9** | **70.3** | 222 | 223 | ## How to Reproduce Our Results 224 | To ensure the fairness of our evaluation, we follow the official implementations to obtain the evaluation results for most of the benchmarks we considered, including LiveCodeBench, CRUXEval-O, CodeEditorBench, and CrossCodeEval. 225 | For HumanEval (zero-shot, multilingual), MBPP (3-shot), and 7 math programming tasks, we follow the widely adopted [evaluation codebase](https://github.com/deepseek-ai/DeepSeek-Coder/tree/main/Evaluation) released by DeepSeek-Coder. 226 | 227 | 228 | # License 229 | The code and weights of the Yi-Coder series models are distributed under the Apache 2.0 license. 230 | 231 | If you create derivative works based on this model, please include the following attribution in your derivative works: 232 | 233 | ```This work is a derivative of [The Yi Series Model You Based On] by 01.AI, licensed under the Apache 2.0 License.``` 234 | 235 | 236 | -------------------------------------------------------------------------------- /assets/coder-readme.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/01-ai/Yi-Coder/a9cf31208e41d2fc46359285fde46494cee72e4b/assets/coder-readme.gif -------------------------------------------------------------------------------- /assets/demo1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/01-ai/Yi-Coder/a9cf31208e41d2fc46359285fde46494cee72e4b/assets/demo1.gif -------------------------------------------------------------------------------- /assets/demo2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/01-ai/Yi-Coder/a9cf31208e41d2fc46359285fde46494cee72e4b/assets/demo2.gif -------------------------------------------------------------------------------- /assets/star.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/01-ai/Yi-Coder/a9cf31208e41d2fc46359285fde46494cee72e4b/assets/star.gif -------------------------------------------------------------------------------- /assets/yicoder.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/01-ai/Yi-Coder/a9cf31208e41d2fc46359285fde46494cee72e4b/assets/yicoder.gif -------------------------------------------------------------------------------- /cookbook/Fine_tune/finetune.md: -------------------------------------------------------------------------------- 1 | ### Fine-tuning 2 | 3 | SWIFT, developed by ModelScope, is a framework designed for training, inference, evaluation, and deploying multimodal large models. It enables a seamless workflow from model training and evaluation to application. Let's explore how to fine-tune the Yi model using SWIFT. 4 | 5 | #### Installation 6 | 7 | Start by cloning the SWIFT repository: 8 | 9 | ```bash 10 | git clone https://github.com/modelscope/swift.git 11 | cd swift 12 | pip install -e '.[llm]' 13 | ``` 14 | 15 | #### Fine-tuning Steps 16 | 17 | We'll use the CLI for fine-tuning. Here's the command: 18 | 19 | ```bash 20 | CUDA_VISIBLE_DEVICES=0 swift sft \ 21 | --model_id_or_path 01ai/Yi-Coder-9B-Chat \ 22 | --dataset AI-ModelScope/blossom-math-v2 \ 23 | --output_dir output \ 24 | ``` 25 | 26 | - `--model_id_or_path`: Replace with the desired model. 27 | - `--dataset`: Specify the dataset for fine-tuning. 28 | - `--output_dir`: Define the directory to save the fine-tuned model. 29 | 30 | For more detailed information on SWIFT, please refer to the official GitHub repository: [https://github.com/modelscope/swift](https://github.com/modelscope/swift). 31 | -------------------------------------------------------------------------------- /cookbook/NL2SQL/NL2SQL.md: -------------------------------------------------------------------------------- 1 | # Yi-Coder: A Powerful Natural Language to SQL Converter 2 | 3 | Welcome to the Yi-Coder tutorial! This document will guide beginners on how to use Yi-Coder for natural language to SQL. Yi-Coder is a powerful tool that can understand natural language queries and translate them into accurate SQL statements. In this experiment, we will use the Yi-Coder-9B-Chat model, a large language model optimized for handling complex database query tasks. 4 | 5 | **Simple question**: ```Count the number of orders for each city.``` 6 | 7 | **SQL**: 8 | ``` 9 | SELECT c.city, 10 | COUNT(o.id) AS number_of_orders 11 | FROM customers c 12 | JOIN orders o ON c.id = o.customer_id 13 | GROUP BY c.city; 14 | ``` 15 | **Complex question**: ```Who are the top 5 users with the most orders?``` 16 | 17 | **SQL**: 18 | ``` 19 | SELECT users.username, 20 | COUNT(orders.id) AS order_count 21 | FROM users 22 | JOIN orders ON users.id = orders.user_id 23 | GROUP BY users.username 24 | ORDER BY order_count DESC 25 | LIMIT 5; 26 | ``` 27 | ## 1. Project Overview 28 | 29 | Our project comprises the following key components: 30 | 31 | 1. **NL2SQLConverter:** Responsible for converting natural language into SQL queries. 32 | 2. **DatabaseManager:** Manages the creation, data insertion, and query execution of an SQLite database. 33 | 3. **Main Function:** Orchestrates the entire process. 34 | 35 | Let's walk through the implementation of each component step-by-step. 36 | 37 | ## 2. Environment Setup 38 | 39 | First, we need to import the necessary libraries and set up logging. If any packages are missing, simply install them using the command `pip install <>`. 40 | 41 | ```python 42 | import re 43 | import sqlparse 44 | import sqlite3 45 | from typing import List, Tuple, Optional, Any 46 | from dataclasses import dataclass 47 | from transformers import AutoTokenizer, AutoModelForCausalLM 48 | import logging 49 | from datetime import datetime, timedelta 50 | import random 51 | 52 | logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') 53 | logger = logging.getLogger(__name__) 54 | ``` 55 | 56 | This code imports all the required libraries and sets up basic logging configuration. Logging will help us track the execution flow of our program. 57 | 58 | 59 | ## 3. Data Structure Definitions 60 | 61 | Next, we define two data classes to represent the database schema and SQL queries. 62 | 63 | ```python 64 | @dataclass 65 | class DatabaseSchema: 66 | tables: List[str] 67 | 68 | @dataclass 69 | class SQLQuery: 70 | raw: str 71 | formatted: str 72 | ``` 73 | 74 | The `DatabaseSchema` class is used to store information about the database tables, while the `SQLQuery` class stores the raw and formatted SQL queries. 75 | 76 | ## 4. NL2SQLConverter Class 77 | 78 | This is the core class of our project, responsible for converting natural language to SQL queries. 79 | 80 | ```python 81 | class NL2SQLConverter: 82 | def __init__(self, model_path: str, device: str = "cuda"): 83 | self.device = device 84 | self.tokenizer = AutoTokenizer.from_pretrained(model_path) 85 | self.model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto").eval() 86 | 87 | def preprocess(self, input_text: str) -> str: 88 | return re.sub(r'[^\w\s]', '', input_text) 89 | 90 | def process_with_large_model(self, input_text: str, schema_info: DatabaseSchema) -> str: 91 | prompt = self._construct_prompt(input_text, schema_info) 92 | messages = self._construct_messages(prompt) 93 | model_inputs = self._prepare_model_inputs(messages) 94 | return self._generate_response(model_inputs) 95 | 96 | def _construct_prompt(self, input_text: str, schema_info: DatabaseSchema) -> str: 97 | return f""" 98 | Given the following database schema: 99 | {schema_info.tables} 100 | 101 | Convert the following natural language query to SQL: 102 | {input_text} 103 | 104 | Please provide only the SQL query without any additional explanation. 105 | """ 106 | 107 | def _construct_messages(self, prompt: str) -> List[dict]: 108 | return [ 109 | {"role": "system", "content": "You are a helpful assistant that converts natural language to SQL."}, 110 | {"role": "user", "content": prompt} 111 | ] 112 | 113 | def _prepare_model_inputs(self, messages: List[dict]) -> Any: 114 | text = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) 115 | return self.tokenizer([text], return_tensors="pt").to(self.device) 116 | 117 | def _generate_response(self, model_inputs: Any) -> str: 118 | generated_ids = self.model.generate( 119 | model_inputs.input_ids, 120 | max_new_tokens=1024, 121 | eos_token_id=self.tokenizer.eos_token_id 122 | ) 123 | generated_ids = [ 124 | output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) 125 | ] 126 | return self.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0].strip() 127 | 128 | def nl2sql(self, input_text: str, schema_info: DatabaseSchema) -> SQLQuery: 129 | cleaned_text = self.preprocess(input_text) 130 | logger.info(f"Cleaned text: {cleaned_text}") 131 | 132 | raw_sql = self.process_with_large_model(cleaned_text, schema_info) 133 | formatted_sql = self.postprocess(raw_sql) 134 | 135 | return SQLQuery(raw=raw_sql, formatted=formatted_sql) 136 | 137 | def postprocess(self, sql_query: str) -> str: 138 | return sqlparse.format(sql_query, reindent=True, keyword_case='upper') 139 | 140 | ``` 141 | 142 | This class contains the following key methods: 143 | 144 | - `__init__`: Initializes the model and tokenizer. 145 | - `preprocess`: Cleans the input text. 146 | - `process_with_large_model`: Processes the input using the large language model. 147 | - `_construct_prompt`: Constructs the prompt for the language model, including schema and query. 148 | - `_construct_messages`: Formats the prompt into a message structure for the model. 149 | - `_prepare_model_inputs`: Prepares the input for the model, including tokenization and moving to the correct device. 150 | - `_generate_response`: Generates the SQL response from the model. 151 | - `nl2sql`: The main conversion method that orchestrates the conversion process. 152 | - `postprocess`: Formats the generated SQL query for readability. 153 | 154 | ## 5. DatabaseManager Class 155 | 156 | This class manages our SQLite database. 157 | 158 | ```python 159 | class DatabaseManager: 160 | def __init__(self, db_path: str = 'ecommerce.db'): 161 | self.db_path = db_path 162 | 163 | def init_database(self): 164 | with sqlite3.connect(self.db_path) as conn: 165 | cursor = conn.cursor() 166 | self._create_tables(cursor) 167 | self._insert_sample_data(cursor) 168 | 169 | def _create_tables(self, cursor: sqlite3.Cursor): 170 | cursor.execute(''' 171 | CREATE TABLE IF NOT EXISTS users 172 | (id INTEGER PRIMARY KEY, username TEXT, email TEXT, registration_date TEXT) 173 | ''') 174 | 175 | cursor.execute(''' 176 | CREATE TABLE IF NOT EXISTS products 177 | (id INTEGER PRIMARY KEY, name TEXT, category TEXT, price REAL, stock INTEGER) 178 | ''') 179 | 180 | cursor.execute(''' 181 | CREATE TABLE IF NOT EXISTS orders 182 | (id INTEGER PRIMARY KEY, user_id INTEGER, order_date TEXT, status TEXT, 183 | FOREIGN KEY (user_id) REFERENCES users(id)) 184 | ''') 185 | 186 | cursor.execute(''' 187 | CREATE TABLE IF NOT EXISTS order_items 188 | (id INTEGER PRIMARY KEY, order_id INTEGER, product_id INTEGER, quantity INTEGER, 189 | FOREIGN KEY (order_id) REFERENCES orders(id), 190 | FOREIGN KEY (product_id) REFERENCES products(id)) 191 | ''') 192 | 193 | cursor.execute(''' 194 | CREATE TABLE IF NOT EXISTS reviews 195 | (id INTEGER PRIMARY KEY, user_id INTEGER, product_id INTEGER, rating INTEGER, comment TEXT, 196 | FOREIGN KEY (user_id) REFERENCES users(id), 197 | FOREIGN KEY (product_id) REFERENCES products(id)) 198 | ''') 199 | 200 | def _insert_sample_data(self, cursor: sqlite3.Cursor): 201 | # Insert users 202 | users = [ 203 | ('john_doe', 'john@example.com', '2023-01-15'), 204 | ('jane_smith', 'jane@example.com', '2023-02-20'), 205 | ('bob_johnson', 'bob@example.com', '2023-03-10'), 206 | ('alice_brown', 'alice@example.com', '2023-04-05'), 207 | ('charlie_davis', 'charlie@example.com', '2023-05-12') 208 | ] 209 | cursor.executemany('INSERT OR REPLACE INTO users (username, email, registration_date) VALUES (?, ?, ?)', users) 210 | 211 | # Insert products 212 | products = [ 213 | ('Laptop', 'Electronics', 999.99, 50), 214 | ('Smartphone', 'Electronics', 599.99, 100), 215 | ('Running Shoes', 'Sports', 79.99, 200), 216 | ('Coffee Maker', 'Home Appliances', 49.99, 75), 217 | ('Book: Python Programming', 'Books', 29.99, 150) 218 | ] 219 | cursor.executemany('INSERT OR REPLACE INTO products (name, category, price, stock) VALUES (?, ?, ?, ?)', products) 220 | 221 | # Insert orders 222 | orders = [ 223 | (1, '2023-06-01', 'Delivered'), 224 | (2, '2023-06-15', 'Shipped'), 225 | (3, '2023-07-01', 'Processing'), 226 | (4, '2023-07-10', 'Delivered'), 227 | (5, '2023-07-20', 'Shipped') 228 | ] 229 | cursor.executemany('INSERT OR REPLACE INTO orders (user_id, order_date, status) VALUES (?, ?, ?)', orders) 230 | 231 | # Insert order items 232 | order_items = [ 233 | (1, 1, 1), 234 | (1, 3, 2), 235 | (2, 2, 1), 236 | (3, 4, 1), 237 | (4, 5, 3), 238 | (5, 1, 1), 239 | (5, 2, 1) 240 | ] 241 | cursor.executemany('INSERT OR REPLACE INTO order_items (order_id, product_id, quantity) VALUES (?, ?, ?)', order_items) 242 | 243 | # Insert reviews 244 | reviews = [ 245 | (1, 1, 5, 'Great laptop, very fast!'), 246 | (2, 2, 4, 'Good phone, but battery life could be better'), 247 | (3, 3, 5, 'Very comfortable shoes'), 248 | (4, 4, 3, 'Decent coffee maker'), 249 | (5, 5, 5, 'Excellent book for learning Python') 250 | ] 251 | cursor.executemany('INSERT OR REPLACE INTO reviews (user_id, product_id, rating, comment) VALUES (?, ?, ?, ?)', reviews) 252 | 253 | def execute_query(self, sql_query: str) -> Optional[List[Tuple]]: 254 | try: 255 | with sqlite3.connect(self.db_path) as conn: 256 | cursor = conn.cursor() 257 | cursor.execute(sql_query) 258 | results = cursor.fetchall() 259 | 260 | if not results: 261 | logger.warning("No results found.") 262 | 263 | return results 264 | except sqlite3.Error as e: 265 | logger.error(f"An error occurred: {e}") 266 | return None 267 | 268 | ``` 269 | 270 | The key methods of this class include: 271 | 272 | - `__init__`: Initializes the database path. 273 | - `init_database`: Initializes the database, creating tables and inserting sample data. 274 | - `_create_tables`: Creates the necessary tables for the e-commerce database. 275 | - `_insert_sample_data`: Populates the tables with sample data. 276 | - `execute_query`: Executes a given SQL query and returns the results. 277 | 278 | 279 | ## 6. Main Function 280 | 281 | Finally, our main function brings all the components together and runs test cases. 282 | 283 | ```python 284 | def main(): 285 | db_manager = DatabaseManager() 286 | db_manager.init_database() 287 | 288 | model_path = '' # Replace with the actual model path 289 | converter = NL2SQLConverter(model_path) 290 | 291 | schema_info = DatabaseSchema(tables=[ 292 | "1. users (id, username, email, registration_date)", 293 | "2. products (id, name, category, price, stock)", 294 | "3. orders (id, user_id, order_date, status)", 295 | "4. order_items (id, order_id, product_id, quantity)", 296 | "5. reviews (id, user_id, product_id, rating, comment)" 297 | ]) 298 | 299 | test_cases = [ 300 | "Show me the top 3 best-selling products", 301 | "List all users who have made a purchase in the last month", 302 | "What is the average rating for products in the Electronics category?", 303 | "Show me the total revenue for each product category", 304 | "Who are the top 5 users with the most orders?" 305 | ] 306 | 307 | for case in test_cases: 308 | sql_query = converter.nl2sql(case, schema_info) 309 | query_results = db_manager.execute_query(sql_query.formatted) 310 | logger.info('-' * 50) 311 | logger.info(f"Query: {case}") 312 | logger.info(f"Final SQL:\n{sql_query.formatted}") 313 | logger.info(f"Query Results:\n{query_results}") 314 | 315 | if __name__ == "__main__": 316 | main() 317 | ``` 318 | 319 | The main function performs the following tasks: 320 | 321 | 1. Initializes the database manager and NL2SQL converter. 322 | 2. Defines the database schema and test cases. 323 | 3. For each test case, performs the conversion and query, and logs the results. 324 | 325 | ## Conclusion 326 | 327 | Through this project, we demonstrated the capabilities of the Yi-Coder-9B-Chat model in handling complex database queries. It can accurately translate natural language queries into SQL statements and perform well in various complex scenarios. Remember to replace `` with the actual path to the Yi-Coder-9B-Chat model. This will allow you to run the code and test the functionality of the NL2SQL converter. 328 | -------------------------------------------------------------------------------- /cookbook/Quantization/quantization.md: -------------------------------------------------------------------------------- 1 | ### Quantize with SWIFT 2 | 3 | SWIFT is a framework open-sourced by modelscope that supports training, inference, evaluation, and deployment of multi-modal large models. It can directly implement the complete chain from model training and evaluation to application. 4 | 5 | Quantization using SWIFT is very convenient, and can be completed in just a few steps. There are many parameters that can be adjusted during quantization, such as the model to be quantized, precision, method, etc. For details, please refer to the [official documentation](https://github.com/modelscope/ms-swift/blob/main/docs/source/Instruction/LLM%E9%87%8F%E5%8C%96%E4%B8%8E%E5%AF%BC%E5%87%BA%E6%96%87%E6%A1%A3.md). 6 | 7 | #### Installation 8 | 9 | First, let's install ms-swift: 10 | 11 | ``````bash 12 | git clone https://github.com/modelscope/swift.git 13 | cd swift 14 | pip install -e '.[llm]' 15 | `````` 16 | 17 | swift supports quantizing models using awq, gptq, bnb, hqq, and eetq techniques. 18 | 19 | You can directly install the quantization method you want to use: 20 | 21 | ``````bash 22 | # Use awq quantization: 23 | # autoawq and cuda versions have a correspondence, please select the version according to `https://github.com/casper-hansen/AutoAWQ` 24 | pip install autoawq -U 25 | 26 | # Use gptq quantization: 27 | # auto_gptq and cuda versions have a correspondence, please select the version according to `https://github.com/PanQiWei/AutoGPTQ#quick-installation` 28 | pip install auto_gptq -U 29 | 30 | # Use bnb quantization: 31 | pip install bitsandbytes -U 32 | 33 | # Use hqq quantization: 34 | # pip install transformer>=4.41 35 | pip install hqq 36 | 37 | # Use eetq quantization: 38 | # pip install transformer>=4.41 39 | 40 | # Refer to https://github.com/NetEase-FuXi/EETQ 41 | git clone https://github.com/NetEase-FuXi/EETQ.git 42 | cd EETQ/ 43 | git submodule update --init --recursive 44 | pip install . 45 | `````` 46 | 47 | If you encounter errors during runtime, you can align the environment (optional): 48 | 49 | ``````bash 50 | # Environment alignment (usually not required to run. If you encounter errors, you can run the following code, the repository uses the latest environment for testing) 51 | pip install -r requirements/framework.txt -U 52 | pip install -r requirements/llm.txt -U 53 | `````` 54 | 55 | #### Start Quantization with swift 56 | 57 | We will use awq and hqq quantization as examples for teaching. 58 | 59 | ##### AWQ Quantization with swift 60 | 61 | awq quantization requires a dataset, you can use a custom dataset here, here we use alpaca-zh alpaca-en sharegpt-gpt4:default as the quantization dataset: 62 | 63 | ``````bash 64 | CUDA_VISIBLE_DEVICES=0 swift export \ 65 | --model_id_or_path 01ai/Yi-Coder-9B-Chat\ 66 | # --model_type yi-coder\ 67 | --quant_bits 4 \ 68 | --dataset alpaca-zh alpaca-en sharegpt-gpt4:default --quant_method awq 69 | `````` 70 | 71 | After quantization, you can also use swift for inference, as follows: 72 | 73 | Replace `model_type` with the type of your model. 74 | 75 | Replace `model_id_or_path` with the path to your quantized model. 76 | 77 | ``````bash 78 | CUDA_VISIBLE_DEVICES=0 swift infer \ 79 | --model_id_or_path 01ai/Yi-Coder-9B-Chat\ 80 | # --model_type yi-coder \ 81 | --model_id_or_path yi-coder-awq-int4 82 | `````` 83 | 84 | ##### HQQ Quantization with swift 85 | 86 | For bnb, hqq, and eetq, we only need to use swift infer for quick quantization and inference. 87 | 88 | You can modify the quantization method with `quant_method`. 89 | 90 | Replace `model_type` with the type of your model. 91 | 92 | Replace `quantization_bit` with the desired quantization bit. 93 | 94 | ``````bash 95 | CUDA_VISIBLE_DEVICES=0 swift infer \ 96 | --model_id_or_path 01ai/Yi-Coder-9B-Chat\ 97 | # --model_type yi-coder \ 98 | --quant_method hqq \ 99 | --quantization_bit 4 100 | `````` 101 | -------------------------------------------------------------------------------- /cookbook/System_prompt/System_prompt.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "gpuType": "L4" 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | }, 13 | "language_info": { 14 | "name": "python" 15 | }, 16 | "accelerator": "GPU" 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "source": [ 22 | "# System Instruction Tutorial - Quick Start: Common Scenarios for Using the Yi-Coder Model\n", 23 | "\n", 24 | "**Yi-Coder**, the most powerful language model under 10 billion parameters, boasts an impressive 128k long text capability. Yi-Coder is a game-changer for developers, pushing the boundaries of code understanding and generation. With its unparalleled accuracy and efficiency, Yi-Coder empowers you to write cleaner, more robust code, faster than ever before.\n", 25 | "\n", 26 | "This tutorial will guide you through a fast-paced exploration of Yi-Coder's capabilities, focusing on practical applications like:\n", 27 | "\n", 28 | "* **Code Completion:** Experience seamless code suggestions as you type, saving valuable time and effort.\n", 29 | "* **Code Insertion:** Effortlessly insert complex code snippets, enhancing your coding workflow.\n", 30 | "* **Repo Q&A:** Ask Yi-Coder questions about your codebase, getting precise answers and insightful feedback.\n", 31 | "\n", 32 | "Get ready to unlock the full potential of Yi-Coder and revolutionize your coding experience. Let's dive in!\n" 33 | ], 34 | "metadata": { 35 | "id": "Bch3RhmuXsy-" 36 | } 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "source": [ 41 | "## 1. Code Completion\n", 42 | "\n", 43 | "Here, we will use the example of \"writing a quick sort algorithm\" to illustrate system prompts.\n", 44 | "\n", 45 | "**Recommended System Prompt:**\n", 46 | "\n", 47 | "```\n", 48 | "You are Yi-Coder, you are exceptionally skilled in programming, coding, and any computer-related issues.\n", 49 | "```" 50 | ], 51 | "metadata": { 52 | "id": "eU4LYptRc_UD" 53 | } 54 | }, 55 | { 56 | "cell_type": "code", 57 | "source": [ 58 | "from transformers import AutoTokenizer, AutoModelForCausalLM\n", 59 | "\n", 60 | "device = \"cuda\" # the device to load the model onto\n", 61 | "model_path = '01-ai/Yi-Coder-9B-Chat'\n", 62 | "# Now you do not need to add \"trust_remote_code=True\"\n", 63 | "tokenizer = AutoTokenizer.from_pretrained(model_path)\n", 64 | "model = AutoModelForCausalLM.from_pretrained(model_path, device_map=\"auto\").eval()\n", 65 | "\n", 66 | "# tokenize the input into tokens\n", 67 | "# Instead of using model.chat(), we directly use model.generate()\n", 68 | "# But you need to use tokenizer.apply_chat_template() to format your inputs as shown below\n", 69 | "prompt = \"Write a quick sort algorithm.\"\n", 70 | "messages = [\n", 71 | " {\"role\": \"system\", \"content\": \"You are Yi-Coder, you are exceptionally skilled in programming, coding, and any computer-related issues.\"},\n", 72 | " {\"role\": \"user\", \"content\": prompt}\n", 73 | "]\n", 74 | "text = tokenizer.apply_chat_template(\n", 75 | " messages,\n", 76 | " tokenize=False,\n", 77 | " add_generation_prompt=True\n", 78 | ")\n", 79 | "model_inputs = tokenizer([text], return_tensors=\"pt\").to(device)\n", 80 | "\n", 81 | "# Directly use generate() and tokenizer.decode() to get the output.\n", 82 | "# Use `max_new_tokens` to control the maximum output length.\n", 83 | "generated_ids = model.generate(\n", 84 | " model_inputs.input_ids,\n", 85 | " max_new_tokens=1024,\n", 86 | " eos_token_id=tokenizer.eos_token_id\n", 87 | ")\n", 88 | "generated_ids = [\n", 89 | " output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)\n", 90 | "]\n", 91 | "\n", 92 | "response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\n", 93 | "print(response)" 94 | ], 95 | "metadata": { 96 | "id": "EHrKmCUZKEzk" 97 | }, 98 | "execution_count": null, 99 | "outputs": [] 100 | }, 101 | { 102 | "cell_type": "markdown", 103 | "source": [ 104 | "## 2. Code Insertion\n", 105 | "\n", 106 | "This scenario demonstrates how Yi-Coder can identify errors and insert the correct code to fix them.\n", 107 | "\n", 108 | "**Recommended System Prompt:**\n", 109 | "\n", 110 | "```\n", 111 | "You are Yi-Coder, you are exceptionally skilled in programming, coding, and any computer-related issues.\n", 112 | "```\n" 113 | ], 114 | "metadata": { 115 | "id": "oNU-ttDhdg3n" 116 | } 117 | }, 118 | { 119 | "cell_type": "code", 120 | "source": [ 121 | "# Instead of using model.chat(), we directly use model.generate()\n", 122 | "# But you need to use tokenizer.apply_chat_template() to format your inputs as shown below\n", 123 | "prompt = \"\"\"\n", 124 | "```python\n", 125 | "def quick_sort(arr):\n", 126 | " if len(arr) <= 1:\n", 127 | " return arr\n", 128 | " else:\n", 129 | " pivot = arr[len(arr) // 2]\n", 130 | " left = [x for x in arr if x < pivot]\n", 131 | "\n", 132 | " right = [x for x in arr if x > pivot]\n", 133 | " return quick_sort(left) + middle + quick_sort(right)\n", 134 | "\n", 135 | "print(quick_sort([3,6,8,10,1,2,1]))\n", 136 | "# Prints \"[1, 1, 2, 3, 6, 8, 10]\"\n", 137 | "```\n", 138 | "Is there a problem with this code?\n", 139 | "\"\"\"\n", 140 | "messages = [\n", 141 | " {\"role\": \"system\", \"content\": \"You are Yi-Coder, you are exceptionally skilled in programming, coding, and any computer-related issues.\"},\n", 142 | " {\"role\": \"user\", \"content\": prompt}\n", 143 | "]\n", 144 | "text = tokenizer.apply_chat_template(\n", 145 | " messages,\n", 146 | " tokenize=False,\n", 147 | " add_generation_prompt=True\n", 148 | ")\n", 149 | "model_inputs = tokenizer([text], return_tensors=\"pt\").to(device)\n", 150 | "\n", 151 | "# Directly use generate() and tokenizer.decode() to get the output.\n", 152 | "# Use `max_new_tokens` to control the maximum output length.\n", 153 | "generated_ids = model.generate(\n", 154 | " model_inputs.input_ids,\n", 155 | " max_new_tokens=1024,\n", 156 | " eos_token_id=tokenizer.eos_token_id\n", 157 | ")\n", 158 | "generated_ids = [\n", 159 | " output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)\n", 160 | "]\n", 161 | "\n", 162 | "response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\n", 163 | "print(response)" 164 | ], 165 | "metadata": { 166 | "id": "KUWhFBW9KeiM" 167 | }, 168 | "execution_count": null, 169 | "outputs": [] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "source": [ 174 | "## 3. Repo Q&A\n", 175 | "\n", 176 | "This scenario focuses on leveraging Yi-Coder to answer questions about an entire codebase.\n", 177 | "\n", 178 | "**Recommended System Prompt:**\n", 179 | "\n", 180 | "```\n", 181 | "You are Yi-Coder, you are exceptionally skilled in programming, coding, and any computer-related issues.\n", 182 | "```" 183 | ], 184 | "metadata": { 185 | "id": "12qI1E_Gd6aJ" 186 | } 187 | }, 188 | { 189 | "cell_type": "code", 190 | "source": [ 191 | "# Instead of using model.chat(), we directly use model.generate()\n", 192 | "# But you need to use tokenizer.apply_chat_template() to format your inputs as shown below\n", 193 | "prompt = \"\"\"\n", 194 | " <>\n", 195 | " {Code Content}\n", 196 | " <>\n", 197 | " {Code Content}\n", 198 | " <>\n", 199 | " {Code Content}\n", 200 | " <>\n", 201 | " {Code Content}\n", 202 | " <>\n", 203 | " {Code Content}\n", 204 | " <>\"\"\"\n", 205 | "messages = [\n", 206 | " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", 207 | " {\"role\": \"user\", \"content\": prompt}\n", 208 | "]\n", 209 | "text = tokenizer.apply_chat_template(\n", 210 | " messages,\n", 211 | " tokenize=False,\n", 212 | " add_generation_prompt=True\n", 213 | ")\n", 214 | "model_inputs = tokenizer([text], return_tensors=\"pt\").to(device)\n", 215 | "\n", 216 | "# Directly use generate() and tokenizer.decode() to get the output.\n", 217 | "# Use `max_new_tokens` to control the maximum output length.\n", 218 | "generated_ids = model.generate(\n", 219 | " model_inputs.input_ids,\n", 220 | " max_new_tokens=1024,\n", 221 | " eos_token_id=tokenizer.eos_token_id\n", 222 | ")\n", 223 | "generated_ids = [\n", 224 | " output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)\n", 225 | "]\n", 226 | "\n", 227 | "response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\n", 228 | "print(response)" 229 | ], 230 | "metadata": { 231 | "id": "qvnW_5OfKmZY" 232 | }, 233 | "execution_count": null, 234 | "outputs": [] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "source": [], 239 | "metadata": { 240 | "id": "KnJ5_csqXGts" 241 | }, 242 | "execution_count": null, 243 | "outputs": [] 244 | } 245 | ] 246 | } -------------------------------------------------------------------------------- /cookbook/Webpage/Webpage.md: -------------------------------------------------------------------------------- 1 | ## Turn Your Ideas into Web Pages with Yi-Coder! 2 | 3 | Yi Coder is a powerful code generation model that lets you build websites with just a simple description. Think of it as a webpage magician, bringing your visions to life! 4 | 5 | 6 | 7 | **Before You Begin:** 8 | 9 | * **Clone the Yi-Coder Repository:** Open your terminal and enter the following command: 10 | 11 | ```bash 12 | git clone https://github.com/01-ai/Yi-Coder.git 13 | ``` 14 | 15 | * **Navigate to the `demo/Webpage` Directory:** 16 | 17 | ```bash 18 | cd Yi-Coder/cookbook/Webpage 19 | ``` 20 | 21 | **Ready to Get Started? It's a Two-Step Process:** 22 | 23 | 1. **Run the Backend Server:** Within the `demo/Webpage` directory, run the `app.py` file: 24 | 25 | ```bash 26 | python app.py 27 | ``` 28 | 29 | 2. **Open the Webpage and Describe Your Vision:** Open `index.html` in your browser. You'll see an input box. Here, you can describe the website you want, like "A red background with a white title that says 'Hello, World!'" Click "Generate" and watch the magic unfold! 30 | 31 | 32 | 33 | **Additional Features:** 34 | 35 | * **Live Preview:** See the generated webpage in real-time, like looking into a mirror! 36 | * **Code View:** Not only can you see the webpage, but you can also view and edit the generated code. Perfect for learning and making adjustments. 37 | * **One-Click Copy/Download:** Directly copy the generated code or download it as an HTML file for easy use! 38 | 39 | **A Few Tips:** 40 | 41 | * The more detailed your description, the more accurately the generated webpage will match your vision. Let your imagination run wild! 42 | * If you're not satisfied with the generated webpage, adjust your description and regenerate until you're happy. 43 | * Yi Coder is constantly learning and improving. We welcome your valuable feedback and suggestions! 44 | 45 | **What are you waiting for? Try Yi Coder and unleash your web page creativity!** 46 | 47 | 48 | **(PS:** For the best experience, consider uploading `app.py` to a cloud server and running `index.html` locally. Yi Coder requires a significant amount of memory, so ensure your computer's configuration is powerful enough!)** 49 | -------------------------------------------------------------------------------- /cookbook/Webpage/app.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | from fastapi.middleware.cors import CORSMiddleware 3 | from pydantic import BaseModel 4 | from transformers import AutoTokenizer, AutoModelForCausalLM 5 | import torch 6 | 7 | app = FastAPI() 8 | 9 | # Configure CORS 10 | origins = [ 11 | "http://localhost", 12 | "http://localhost:63342", 13 | "http://localhost:8080", 14 | "*", # Allow all origins for demonstration purposes. Be cautious in production. 15 | ] 16 | 17 | app.add_middleware( 18 | CORSMiddleware, 19 | allow_origins=origins, 20 | allow_credentials=True, 21 | allow_methods=["*"], 22 | allow_headers=["*"], 23 | ) 24 | 25 | model_path = "01-ai/Yi-Coder-9B-Chat" 26 | device = "cuda" if torch.cuda.is_available() else "cpu" 27 | tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) 28 | model = AutoModelForCausalLM.from_pretrained( 29 | model_path, 30 | torch_dtype=torch.bfloat16, 31 | low_cpu_mem_usage=True, 32 | trust_remote_code=True 33 | ).to(device).eval() 34 | 35 | 36 | class RequestBody(BaseModel): 37 | prompt: list 38 | 39 | 40 | # System instructions 41 | SYSTEM_INSTRUCTIONS = """ 42 | You are a helpful AI assistant, please adhere to the following guidelines: 43 | 1. When you need to provide HTML code, make sure you provide complete and fully functional HTML code. Include the HTML code in the ```html and ``` tags. Ensure that the generated HTML code is self-contained and can be run independently. 44 | 2. Strictly follow the user instructions and output all code as required. 45 | 3. Your name is the Yi-Coder model. 46 | 4. Include all necessary CSS and JavaScript in the HTML file, making sure not to write them separately. 47 | 5. You can also communicate with users normally. 48 | """ 49 | 50 | @app.post("/generate") 51 | async def generate_text(request: RequestBody): 52 | print(request.prompt) 53 | conversation = [{"role": "system", "content": SYSTEM_INSTRUCTIONS}] + request.prompt 54 | inputs = tokenizer.apply_chat_template( 55 | conversation, 56 | add_generation_prompt=True, 57 | tokenize=True, 58 | return_tensors="pt", 59 | return_dict=True 60 | ).to(device) 61 | 62 | with torch.no_grad(): 63 | outputs = model.generate( 64 | **inputs, 65 | max_new_tokens=3096, 66 | do_sample=True, 67 | temperature=0.7, 68 | top_p=0.95, 69 | eos_token_id=tokenizer.eos_token_id 70 | ) 71 | outputs = outputs[:, inputs['input_ids'].shape[1]:] 72 | generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) 73 | 74 | return {"generated_text": generated_text} 75 | 76 | 77 | if __name__ == "__main__": 78 | import uvicorn 79 | 80 | uvicorn.run(app, host="0.0.0.0", port=8000) 81 | -------------------------------------------------------------------------------- /cookbook/Webpage/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | HTML Generator 7 | 244 | 245 | 246 | 247 | 248 |
249 | 258 |
259 |
260 |

Put your ideas into action!

261 |
262 | 263 | 264 |
265 |
266 |
267 | 274 | 275 |
276 | 283 |
284 |
285 | 428 | 429 | -------------------------------------------------------------------------------- /cookbook/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | torch 2 | transformers>=4.36.2 3 | safetensors 4 | accelerate 5 | vllm 6 | 7 | --------------------------------------------------------------------------------