Local로 Lama 3.2 사용하여 그로서리 목록 분류 및 레시피 생성하기###


“`html

Lama 3.2를 사용하여 그로서리 목록 분류 및 레시피 생성하기

소개

이 블로그 포스트에서는 로컬에서 실행되는 Lama 3.2를 사용하여 그로서리 목록의 항목을 분류하고, 해당 항목을 기반으로 레시피를 생성하는 방법을 살펴보겠습니다. 이 방법은 API 키 비용을 지출하지 않고도 강력한 AI 애플리케이션을 구축하는 데 사용할 수 있습니다.

방법

1. 필요한 라이브러리 설치

먼저 Lama 3.2와 기타 필요한 라이브러리를 설치합니다.

bash
pip install llama

2. 그로서리 목록 로드

그런 다음 그로서리 목록을 텍스트 파일에서 로드합니다.

3. 항목 분류

Lama 3.2를 사용하여 그로서리 목록의 항목을 분류합니다.

4. API 호출 시뮬레이션

가격 및 영양 가치와 같은 추가 메타데이터를 가져오기 위해 각 항목에 대한 API 호출을 시뮬레이션합니다.

5. 레시피 생성

분류된 항목을 기반으로 임의의 카테고리를 선택하고, 해당 항목을 사용하여 Lama 3.2로 레시피를 생성합니다.

6. 결과 저장

분류된 항목, 메타데이터 및 레시피를 텍스트 파일에 저장합니다.

예제

다음 코드는 앞에서 설명한 단계를 구현하는 간단한 예입니다.

“`python
import llama
import json

그로서리 목록 로드

with open(“grocery_list.txt”, “r”) as f:
grocery_list = f.read().splitlines()

항목 분류

lama_client = llama.Client()
prompt = “You are an assistant that categorizes grocery items. Return the result only as a valid JSON. Do not include any explanations, greetings, or additional text. Ensure the JSON is properly formatted. The grocery list is:” + “\n”.join(grocery_list)
messages = [{“role”: “user”, “content”: prompt}]
response = lama_client.chat(model=”lama-large”, messages=messages)[‘candidates’][0][‘content’]
categorized_items = json.loads(response)

API 호출 시뮬레이션

price_and_nutrition_data = {item: {“price”: 0, “calories”: 0, “fat”: 0, “protein”: 0} for item in categorized_items}

레시피 생성

category = random.choice(list(categorized_items.keys()))
recipe_prompt = “As a chef, create a recipe in the category {}. Provide the recipe as a valid JSON object. Do not include any text before or after the JSON. Use double quotes for all strings and ensure the JSON is properly formatted.”.format(category)
messages = [{“role”: “user”, “content”: recipe_prompt}]
response = lama_client.chat(model=”lama-large”, messages=messages)[‘candidates’][0][‘content’]
recipe = json.loads(response)

결과 저장

with open(“results.txt”, “w”) as f:
f.write(“Categorized Items:\n”)
json.dump(categorized_items, f, indent=4)
f.write(“\n\nPrice and Nutrition Data:\n”)
json.dump(price_and_nutrition_data, f, indent=4)
f.write(“\n\nRecipe:\n”)
json.dump(recipe, f, indent=4)
“`

결론

Lama 3.2를 사용하여 그로서리 목록을 분류하고, 레시피를 생성하는 것은 강력한 AI 애플리케이션을 구축하는 데 사용할 수 있는 간단하고 효과적인 방법입니다. 이 방법은 로컬에서 실행되므로 비용이 들지 않으며 유연하고 사용자 정의가 가능합니다.


Leave a Reply

Your email address will not be published. Required fields are marked *