Example of including an image + prompt to gpt-4o?

オープン
#216 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
25/100
issue の種類
ドキュメント
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
swift
領域
documentation

調査の方向性

Start by checking the package's existing examples and the ChatQuery.ChatCompletionMessageParam API used in the issue, especially how a user message can carry both text and an image for gpt-4o. Done looks like an example showing an image from assets or the device sent with a prompt; the issue has had no comment for over two years, so confirm the request is still relevant before starting.

索引モデルが issue の本文から書いたものです。

説明

Hi.

Pretty new to both Swift and this package.

Can anyone include an example of how to supply an image from assets or uploaded from the device with a prompt to the gpt-4o endpoint?

My current progress is below, but I feel like I'm doing something terribly wrong.

Only requirement is making it work at this point :D.

Current Error:
image

Current Code:

private func sendDataToAPI() async {
        let washRoutine = UserDefaults.standard.string(forKey: "washRoutine") ?? ""
        let beforeBedRoutine = UserDefaults.standard.string(forKey: "beforeBedRoutine") ?? ""
        let otherHairCare = UserDefaults.standard.string(forKey: "otherHairCare") ?? ""
        
        var messages: [ChatQuery.ChatCompletionMessageParam] = [
            .user(.init(content: .string("Here are my current hair care routines:"))),
            .user(.init(content: .string("Wash Routine: \(washRoutine)"))),
            .user(.init(content: .string("Before Bed Routine: \(beforeBedRoutine)"))),
            .user(.init(content: .string("Other Hair Care: \(otherHairCare)")))
        ]
        
        if let image = UIImage(named: "curly_1"),
           let imageData = image.jpegData(compressionQuality: 1.0) {
            let base64String = imageData.base64EncodedString()
            let imageUrl = "data:image/jpeg;base64,\(base64String)"
            let imageParam = ChatQuery.ChatCompletionMessageParam.ChatCompletionUserMessageParam.init(content: .string(imageUrl))
            messages.append(.user(imageParam))
        }
        
        let openAI = OpenAI(apiToken: "<api_token>")
        let query = ChatQuery(messages: messages, model: .gpt4_o)
        
        do {
            let result = try await openAI.chats(query: query)
            let content = result.choices.first?.message.content?.string
            let tokenCount = result.usage?.promptTokens ?? 0
            
            DispatchQueue.main.async {
                self.apiResult = "\nPrompt tokens: \(tokenCount)\n\n\(content ?? "No content")"
            }
            
        } catch {
            DispatchQueue.main.async {
                self.apiResult = "Error fetching chats: \(error.localizedDescription)"
            }
        }
    }
主要言語
Swift
スター
2.9k
フォーク
519
平均マージ
1日 17時間
マージ済み PR(30日)
10

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

MacPaw/OpenAI のほかの issue

MacPaw/OpenAI の issue をすべて見る

似ている issue

Swift の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。