调用 Vertex AI Endpoint for Swift(IOS 应用程序)

问题描述 投票:0回答:1

我在 Vertex AI 中创建了一个调整模型并将其部署到端点中。我想在我的 Swift IOS 应用程序中使用调整后的模型,但我不知道如何操作。

有什么方法可以使用 Swift 代码调用它吗?或者还有其他方法吗?

我尝试搜索所有文档,但我不知道部署端点后该怎么办。

swift google-cloud-endpoints endpoint google-cloud-vertex-ai
1个回答
0
投票

您可以使用 REST 方法向部署在 vertexai 上的微调模型发送请求。

您可以使用类似的方法来使用 Swift 发送请求

import Foundation

// Replace these placeholders with your actual values
let tuningJobRegion = "YOUR_TUNING_JOB_REGION"
let projectId = "YOUR_PROJECT_ID"
let endpointId = "YOUR_ENDPOINT_ID"
let urlString = "https://\(tuningJobRegion)-aiplatform.googleapis.com/v1/projects/\(projectId)/locations/\(tuningJobRegion)/endpoints/\(endpointId):generateContent"
guard let url = URL(string: urlString) else {
    print("Invalid URL")
    return
}

// Create a URL request and specify the HTTP method as POST
var request = URLRequest(url: url)
request.httpMethod = "POST"

// Define the JSON body
let parameters: [String: Any] = [
    "contents": [
        [
            "role": "USER",
            "parts": [
                "text": "Why is the sky blue?"
            ]
        ]
    ],
    "generation_config": [
        "temperature": 0.7,    // Replace with your value for TEMPERATURE
        "topP": 0.9,           // Replace with your value for TOP_P
        "topK": 50,            // Replace with your value for TOP_K
        "maxOutputTokens": 100 // Replace with your value for MAX_OUTPUT_TOKENS
    ]
]

do {
    request.httpBody = try JSONSerialization.data(withJSONObject: parameters)
} catch {
    print("Failed to serialize JSON: \(error.localizedDescription)")
    return
}

// Set the necessary HTTP headers
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer YOUR_ACCESS_TOKEN", forHTTPHeaderField: "Authorization")

let session = URLSession.shared

// Create a data task to send the data to the API
let task = session.dataTask(with: request) { data, response, error in
    if let error = error {
        print("Error: \(error.localizedDescription)")
        return
    }
    
    // Ensure that we have data
    guard let data = data else {
        print("No data returned")
        return
    }
    
    // Try to decode the data into a usable format (e.g., JSON)
    do {
        if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
            print("JSON response: \(json)")
        }
    } catch {
        print("Failed to decode JSON: \(error.localizedDescription)")
    }
}

task.resume()

您可以在文档中找到每件事的 REST 方法 就像这个

© www.soinside.com 2019 - 2024. All rights reserved.