Swift OpenAI API Integration: Generating Multiple Responses with Role-Specific Content
import Foundation
func requestOpenAI(prompt: String) { let parameters: [String: Any] = [ "prompt": prompt, "max_tokens": 50, "temperature": 0.8, "n": 3 ]
// Make the API request to OpenAI
// Assuming the API response is stored in a variable called "response"
if let response = response as? [String: Any],
let choices = response["choices"] as? [[String: Any]] {
for choice in choices {
if let text = choice["text"] as? String {
// Check the length of the response
let length = text.count
if length <= 150 {
let psychologistResponse = text
// Use the psychologistResponse for further processing
print('Psychologist Response: \(psychologistResponse)')
} else if length <= 230 {
let positiveResponse = text
// Use the positiveResponse for further processing
print('Positive Response: \(positiveResponse)')
} else {
let imaginedScene = text
// Use the imaginedScene for further processing
print('Imagined Scene: \(imaginedScene)')
}
}
}
}
}
// Example usage let userText = "I'm feeling really down today" requestOpenAI(prompt: userText)
原文地址: https://www.cveoy.top/t/topic/qqSv 著作权归作者所有。请勿转载和采集!