Skip to content

Latest commit

 

History

History
134 lines (94 loc) · 5.03 KB

File metadata and controls

134 lines (94 loc) · 5.03 KB

Phi-4-mini-mmを使ったコード生成

Phi-4-miniはPhiファミリーの強力なコーディング能力を引き継いでいます。Promptを使ってコーディングに関する質問ができます。もちろん、強力な推論能力が加わったことで、要件に応じたプロジェクトの生成など、より高度なコーディング能力を持っています。例えば、以下のように要件に基づいてプロジェクトを生成できます。

要件

ショッピングカートアプリを作成する

  • 以下のメソッドを持つAPI Restを作成する:
    • ページのオフセットとリミットを使ってビールのリストを取得する。
    • idでビールの詳細を取得する。
    • 名前、説明、キャッチフレーズ、フードペアリング、価格でビールを検索する。
  • メインページに商品リストを作成する。
    • 商品を絞り込むための検索バーを作成する。
    • ユーザーが商品をクリックしたら説明ページに遷移する。
  • (オプション)価格で商品を絞り込むスライサーを作成する。
  • ショッピングカートを作成する。
    • 商品をカートに追加する。
    • 商品をカートから削除する。
    • カート内の商品合計金額を計算する。

サンプルコード - 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`
                   ```python

                   ```
                   .......
               

               ## Frontend
                 
                   #### `templates/index.html`
                   ```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」を使用して翻訳されました。正確性を期しておりますが、自動翻訳には誤りや不正確な部分が含まれる可能性があります。原文の言語によるオリジナル文書が正式な情報源とみなされるべきです。重要な情報については、専門の人間による翻訳を推奨します。本翻訳の利用により生じた誤解や誤訳について、当方は一切の責任を負いかねます。