{ "title": "Swift OpenAI API: Generating Personalized Responses with Role-Based Content", "description": "Learn how to use the OpenAI API in Swift to generate personalized responses with predefined roles. This code example demonstrates how to structure responses from a psychologist, positive messages, and imagined visuals.", "keywords": "OpenAI, API, Swift, code example, chatbot, personalized responses, roles, psychologist, positive message, imagined visual", "content": ""import Foundation\n\nfunc getOpenAIResponse(prompt: String) {\n let parameters: [String: Any] = [\n "prompt": prompt,\n "max_tokens": 50,\n "temperature": 0.8,\n "n": 3\n ]\n \n // Call OpenAI API to get response\n // ...\n // Assuming the response is stored in the variable 'openAIResponse'\n \n // Extracting the individual responses\n guard let responses = openAIResponse["choices"] as? [[String: Any]] else {\n print("Invalid response from OpenAI")\n return\n }\n \n // Loop through the responses\n for (index, response) in responses.enumerated() {\n guard let text = response["text"] as? String else {\n print("Invalid response text from OpenAI")\n continue\n }\n \n // Set different objects based on the response\n switch index {\n case 0:\n // Set the first response as the reply from the psychologist\n let psychologistReply = text.prefix(150)\n print("Psychologist's reply: (psychologistReply)")\n break\n case 1:\n // Set the second response as a positive message from the psychologist\n let positiveMessage = text.prefix(230)\n print("Positive message: (positiveMessage)")\n break\n case 2:\n // Set the third response as an imagined visual\n let imaginedVisual = text\n print("Imagined visual: (imaginedVisual)")\n break\n default:\n break\n }\n }\n}\n\n// Example usage\nlet userMessage = "I'm feeling really down today, can you help me?" getOpenAIResponse(prompt: userMessage)" }

Swift OpenAI API: Generating Personalized Responses with Role-Based Content

原文地址: https://www.cveoy.top/t/topic/qqSp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录