Chapter 56
使用 Phi-4-mini-mm 生成代码
使用 Phi-4-mini-mm 生成代码
Phi-4-mini 延续了 Phi 家族强大的编码能力。你可以通过 Prompt 提出与编码相关的问题。当然,加入了强大的推理能力后,它的编码能力更强,比如根据需求生成项目。例如,根据需求生成项目,如:
需求
创建一个购物车应用
- 创建一个包含以下方法的 API Rest:
- 使用分页偏移和限制获取啤酒列表。
- 根据 id 获取啤酒详情。
- 按名称、描述、标语、食物搭配和价格搜索啤酒。
- 在主页创建产品列表。
- 创建搜索栏以筛选产品。
- 用户点击产品时跳转到描述页面。
- (可选)使用滑块按价格筛选产品。
- 创建购物车。
- 将产品添加到购物车。
- 从购物车中移除产品。
- 计算购物车中产品的总价。
示例代码 - Python
python
import requests
import torch
from PIL import Image
import soundfile
from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig,pipeline,AutoTokenizer
model_path = 'Your Phi-4-mini-mm-instruct'
kwargs = {}
kwargs['torch_dtype'] = torch.bfloat16
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True,
torch_dtype='auto',
_attn_implementation='flash_attention_2',
).cuda()
generation_config = GenerationConfig.from_pretrained(model_path, 'generation_config.json')
user_prompt = '<|user|>'
assistant_prompt = '<|assistant|>'
prompt_suffix = '<|end|>'
requirement = """
Create a Shopping Cart App
- Create an API Rest with the following methods:
- Get a list of beers using page offset and limit.
- Get beer details by id.
- Search for beer by name, description, tagline, food pairings, and price.
- Create a list of products on the main page.
- Create a search bar to filter products.
- Navigate to the description page when the user clicks on a product.
- (Optional) Slicer to filter products by price.
- Create a shopping cart.
- Add products to the cart.
- Remove products from the cart.
- Calculate the total price of the products in the cart."""
note = """
Note:
1. Use Python Flask to create a Repository pattern based on the following structure to generate the files
|- models
|- controllers
|- repositories
|- views
2. For the view page, please use SPA + VueJS + TypeScript to build
3. Firstly use markdown to output the generated project structure (including directories and files), and then generate the file names and corresponding codes step by step, output like this
## Project Structure
|- models
| - user.py
|- controllers
| - user_controller.py
|- repositories
| - user_repository.py
|- templates
| - index.html
## Backend
#### `models/user.py`
```
```
.......
## Frontend
#### `templates/index.html`
```
```
......."""
prompt = f'{user_prompt}Please create a project with Python and Flask according to the following requirements:\n{requirement}{note}{prompt_suffix}{assistant_prompt}'
inputs = processor(prompt, images=None, return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=2048,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(response)免责声明:
本文件使用 AI 翻译服务 Co-op Translator 进行翻译。虽然我们力求准确,但请注意,自动翻译可能包含错误或不准确之处。原始文件的母语版本应被视为权威来源。对于重要信息,建议采用专业人工翻译。对于因使用本翻译而产生的任何误解或误释,我们不承担任何责任。
