-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchatbot.swift
More file actions
53 lines (46 loc) · 1.7 KB
/
chatbot.swift
File metadata and controls
53 lines (46 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import Foundation
// Define the API endpoint and your API key
let apiKey = "your_openai_api_key"
let url = URL(string: "https://api.openai.com/v1/chat/completions")!
// Create the headers for the API request
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
// Define the request body
let requestBody: [String: Any] = [
"model": "gpt-3.5-turbo", // or you can use "gpt-4" if preferred
"messages": [
["role": "system", "content": "You are a helpful assistant."],
["role": "user", "content": "Hello!"]
]
]
do {
let jsonData = try JSONSerialization.data(withJSONObject: requestBody)
request.httpBody = jsonData
} catch {
print("Error serializing JSON: \(error)")
}
// Make the API request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Error making request: \(error)")
return
}
if let data = data {
do {
// Parse the response
if let jsonResponse = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let choices = jsonResponse["choices"] as? [[String: Any]],
let firstChoice = choices.first,
let message = firstChoice["message"] as? [String: Any],
let content = message["content"] as? String {
// Print the response from the chatbot
print("Chatbot Response: \(content)")
}
} catch {
print("Error parsing JSON response: \(error)")
}
}
}
task.resume()