-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from mst-mkt/fix/ai_#65
AIまわりの改善
- Loading branch information
Showing
7 changed files
with
627 additions
and
502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
FRONTEND_BASE_URL = http://localhost:5173 | ||
OPENAI_API_KEY = | ||
OPENAI_BASE_URL = | ||
OPENAI_BASE_URL = https://gateway.ai.cloudflare.com/v1/aff21982f7963fd8f2d9d373bd3b0f6f/chefcam/openai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,31 @@ | ||
import { HumanMessage } from '@langchain/core/messages' | ||
import type { ChatOpenAI } from '@langchain/openai' | ||
import { type LanguageModel, generateObject } from 'ai' | ||
import { z } from 'zod' | ||
import { fileToBase64 } from '../../utils/fileToBase64' | ||
|
||
const ingredientsSchema = z.object({ | ||
data: z.array(z.string()).describe('食材のリスト'), | ||
}) | ||
|
||
export const imageToFoods = async (ai: ChatOpenAI, file: File) => { | ||
export const imageToFoods = async (model: LanguageModel, file: File) => { | ||
const imageUrl = await fileToBase64(file) | ||
|
||
const structuredLlm = ai.withStructuredOutput(ingredientsSchema, { | ||
name: 'food_detection', | ||
}) | ||
|
||
const message = new HumanMessage({ | ||
content: [ | ||
const { object } = await generateObject({ | ||
model, | ||
messages: [ | ||
{ | ||
type: 'text', | ||
text: `# 以下のステップに従ってください、ステップが全て完了するまで出力は決定しないでください。: | ||
role: 'user', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: `# 以下のステップに従ってください、ステップが全て完了するまで出力は決定しないでください。: | ||
## ステップ1: この画像に含まれている食材を日本語かつ短い単語で具体的にリストアップしてください。: | ||
## ステップ2: リストアップした食材から以下の基準でフィルタリングしてください: | ||
- 料理の材料として直接使用できるものだけを含めます。 | ||
- 曖昧な表現(例:「冷凍食品」)は避け、具体的な食材名を使用してください。 | ||
## ステップ3: 食材のリストを提出してください。`, | ||
}, | ||
{ type: 'image', image: imageUrl }, | ||
], | ||
}, | ||
{ type: 'image_url', image_url: { url: imageUrl } }, | ||
], | ||
schema: z.object({ data: z.array(z.string()).describe('食材のリスト') }), | ||
}) | ||
|
||
const { data } = await structuredLlm.invoke([message]) | ||
return data | ||
return object.data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.