You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Your updated code does not allow for chats and you now have some crazy requirements an API string. After banging my head over and over again to get your stuff to work I gave up.
But I decided to write an API of my own and share it.
Cheers,
Robert Clancy
//
// ViewController.swift
// Open AI API
//
// Created by Robert Clancy on 30.07.2023.
//
import Cocoa
import WebKit
import Foundation
var lastResponse = ""
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
var results = ""
var openAICommand = ""
Task {
do {
try await postMessage(apiKey: "your key", content: openAICommand, role: "user", model: "gpt-3.5-turbo")
} catch {
print("An error occurred: \(error)")
}
}
}
@IBOutlet weak var webViewMain: WKWebView!
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
struct OpenAIResponse: Codable {
let id: String
let object: String
let created: Int
let model: String
let usage: Usage
let choices: [Choice]
}
struct Usage: Codable {
let prompt_tokens: Int
let completion_tokens: Int
let total_tokens: Int
}
struct Choice: Codable {
let message: Message
let finish_reason: String
}
struct Message: Codable {
let role: String
let content: String
}
struct CompletionRequest: Codable {
let model: String
let max_tokens: Int
let messages: [Message]
let temperature: Double
let stream: Bool
}
func postMessage(apiKey: String, content: String, role: String, model: String, temperature: Double = 0.7) async throws {
let url = URL(string: "https://api.openai.com/v1/chat/completions")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
let message = Message(role: role, content: content)
let requestObject = CompletionRequest(model: model, max_tokens: 4000, messages: [message], temperature: temperature, stream: false)
let requestData = try JSONEncoder().encode(requestObject)
request.httpBody = requestData
do {
let (data, _) = try await URLSession.shared.data(for: request)
if let jsonString = String(data: data, encoding: .utf8) {
print(jsonString) // This prints the raw JSON response
}
let responseObject = try JSONDecoder().decode(OpenAIResponse.self, from: data)
print(responseObject.choices[0].message.content) // Handle the response as needed
lastResponse = responseObject.choices[0].message.content
} catch {
print("Error: \(error)")
}
}
}
The text was updated successfully, but these errors were encountered:
Describe the bug
Your updated code does not allow for chats and you now have some crazy requirements an API string. After banging my head over and over again to get your stuff to work I gave up.
But I decided to write an API of my own and share it.
Cheers,
Robert Clancy
//
// ViewController.swift
// Open AI API
//
// Created by Robert Clancy on 30.07.2023.
//
import Cocoa
import WebKit
import Foundation
var lastResponse = ""
class ViewController: NSViewController {
}
The text was updated successfully, but these errors were encountered: