Skip to content

Latest commit

 

History

History
142 lines (103 loc) · 5.09 KB

File metadata and controls

142 lines (103 loc) · 5.09 KB

Phi-4-mini-mm을 사용하여 코드 생성하기

Phi-4-mini는 Phi Family의 강력한 코딩 능력을 이어받았습니다. 코딩 관련 질문을 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를 사용하여 번역되었습니다. 정확성을 위해 노력하고 있지만, 자동 번역에는 오류나 부정확성이 포함될 수 있습니다. 원본 문서(원어로 작성된 문서)를 신뢰할 수 있는 권위 있는 출처로 간주해야 합니다. 중요한 정보에 대해서는 전문적인 인간 번역을 권장합니다. 이 번역을 사용함으로써 발생하는 오해나 잘못된 해석에 대해 당사는 책임을 지지 않습니다.