使用 OpenAI API 获取基于用户陈述的治疗建议
let headers: HTTPHeaders = [
"Authorization": "Bearer "+"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",
"Content-Type": "application/json"
]
let message : [[String: String]] = [
["role": "system",
"content": "'用户陈述 1', respond with one sentence the way a therapist might respond to acknowledge this person's feelings, in under 150 characters"],
["role": "system",
"content": "'用户陈述 2', write me an affirmation in under 230 characters"],
["role": "system",
"content": "'用户陈述 3', give me a visualization that will help me feel better in around 400 characters. please make the visualization something that a therapist might use to make you feel better."]
]
let parameters: Parameters = [
"model": "gpt-3.5-turbo",
"messages": message,
]
print(parameters)
showLoad()
AF.request("https://api.openai.com/v1/chat/completions",method: .post,parameters:parameters,encoding: JSONEncoding.default,headers: headers).response(completionHandler: {response in
hidLoad()
if let data = response.data {
do {
if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
self.resultLabel.alpha = 1;
let arr = isArray(json["choices"])
if(arr.count>0)
{
let data = isDic(arr[0])
let lastDic = isDic(data["message"]);
self.resultLabel.text = isString(lastDic["content"])
self.resultLabel.width = SCREEN_WIDTH-wid(20)
self.resultLabel.sizeToFit()
}
// 转换成功,可以使用json进行操作
print(json);
}
} catch {
// 转换失败,处理错误
}
}
print("ttt=(response)")
})
请将代码中的 '用户陈述 1'、'用户陈述 2' 和 '用户陈述 3' 替换为实际的用户陈述。
原文地址: https://www.cveoy.top/t/topic/qqPT 著作权归作者所有。请勿转载和采集!