Skip to content

Commit

Permalink
feat: add new client api
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Aug 28, 2023
1 parent 898be15 commit 91a054b
Show file tree
Hide file tree
Showing 63 changed files with 3,133 additions and 30 deletions.
3 changes: 1 addition & 2 deletions ReactNativeOpenAI.podspec
Expand Up @@ -11,13 +11,12 @@ Pod::Spec.new do |s|
s.license = package["license"]
s.authors = package["author"]

s.platforms = { :ios => "13.0" }
s.platforms = { :ios => "15.0" }
s.source = { :git => "https://github.com/candlefinance/react-native-openai.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm,swift}"

s.dependency "React-Core"
s.dependency "OpenAIKit"

# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile
Expand Up @@ -5,7 +5,7 @@ require Pod::Executable.execute_command('node', ['-p',
{paths: [process.argv[1]]},
)', __dir__]).strip

platform :ios, '13.0'
platform :ios, '15.0'
prepare_react_native_project!

# If you are using a `react-native-flipper` your iOS build will fail when `NO_FLIPPER=1` is set.
Expand Down
8 changes: 2 additions & 6 deletions example/ios/Podfile.lock
Expand Up @@ -71,7 +71,6 @@ PODS:
- fmt (6.2.1)
- glog (0.3.5)
- libevent (2.1.12)
- OpenAIKit (1.5.0)
- OpenSSL-Universal (1.1.1100)
- RCT-Folly (2021.07.22.00):
- boost
Expand Down Expand Up @@ -450,7 +449,6 @@ PODS:
- React-logger (= 0.72.3)
- React-perflogger (= 0.72.3)
- ReactNativeOpenAI (0.1.0):
- OpenAIKit
- React-Core
- SocketRocket (0.6.1)
- Yoga (1.14.0)
Expand Down Expand Up @@ -534,7 +532,6 @@ SPEC REPOS:
- FlipperKit
- fmt
- libevent
- OpenAIKit
- OpenSSL-Universal
- SocketRocket
- YogaKit
Expand Down Expand Up @@ -636,7 +633,6 @@ SPEC CHECKSUMS:
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenAIKit: cd69fa5a5585e13ab416042cb98fd31897dcf3f8
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: a2faf4bad4e438ca37b2040cb8f7799baa065c18
Expand Down Expand Up @@ -670,11 +666,11 @@ SPEC CHECKSUMS:
React-runtimescheduler: af0b24628c1d543a3f87251c9efa29c5a589e08a
React-utils: bcb57da67eec2711f8b353f6e3d33bd8e4b2efa3
ReactCommon: d7d63a5b3c3ff29304a58fc8eb3b4f1b077cd789
ReactNativeOpenAI: 2bf70756d77ed1565720a14144e953abe7d155bd
ReactNativeOpenAI: 93da4285ad4a32ab357523643ec91e8d4d1513e8
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 8796b55dba14d7004f980b54bcc9833ee45b28ce
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

PODFILE CHECKSUM: de38e1abd726248643b04e967f81da73b9a3b577
PODFILE CHECKSUM: 19d89d5a431df00f3ce6b4f3999bfda93d6ad385

COCOAPODS: 1.12.1
36 changes: 36 additions & 0 deletions ios/OpenAIKit/API.swift
@@ -0,0 +1,36 @@
import Foundation

public struct API {
public let scheme: Scheme
public let host: String
public let path: String?

public init(
scheme: API.Scheme,
host: String,
pathPrefix path: String? = nil
) {
self.scheme = scheme
self.host = host
self.path = path
}
}

extension API {
public enum Scheme {
case http
case https
case custom(String)

var value: String {
switch self {
case .http:
return "http"
case .https:
return "https"
case .custom(let scheme):
return scheme
}
}
}
}
81 changes: 81 additions & 0 deletions ios/OpenAIKit/Audio/AudioProvider.swift
@@ -0,0 +1,81 @@
//
// AudioProvider.swift
//
//
// Created by Dylan Shine on 3/19/23.
//

import Foundation

public struct AudioProvider {

private let requestHandler: RequestHandler

init(requestHandler: RequestHandler) {
self.requestHandler = requestHandler
}

/**
Create transcription BETA
POST
https://api.openai.com/v1/audio/transcriptions
Transcribes audio into the input language.
*/
public func transcribe(
file: Data,
fileName: String,
mimeType: MIMEType.Audio,
model: ModelID = Model.Whisper.whisper1,
prompt: String? = nil,
responseFormat: String? = nil,
temperature: Double? = nil,
language: Language? = nil
) async throws -> Transcription {

let request = CreateTranscriptionRequest(
file: file,
fileName: fileName,
mimeType: mimeType,
model: model,
prompt: prompt,
responseFormat: responseFormat,
temperature: temperature,
language: language
)

return try await requestHandler.perform(request: request)
}

/**
Create translation BETA
POST
https://api.openai.com/v1/audio/translations
Translates audio into into English.
*/
public func translate(
file: Data,
fileName: String,
mimeType: MIMEType.Audio,
model: ModelID = Model.Whisper.whisper1,
prompt: String? = nil,
responseFormat: String? = nil,
temperature: Double? = nil
) async throws -> Translation {

let request = CreateTranslationRequest(
file: file,
fileName: fileName,
mimeType: mimeType,
model: model,
prompt: prompt,
responseFormat: responseFormat,
temperature: temperature
)

return try await requestHandler.perform(request: request)
}
}
55 changes: 55 additions & 0 deletions ios/OpenAIKit/Audio/CreateTranscriptionRequest.swift
@@ -0,0 +1,55 @@
import Foundation

struct CreateTranscriptionRequest: Request {
let method: HTTPMethod = .post
let path = "/v1/audio/transcriptions"
let body: Data?
private let boundary = UUID().uuidString

var headers: HTTPHeaders {
var headers = HTTPHeaders()
headers.add(name: .contentType, value: "multipart/form-data; boundary=\(boundary)")
return headers
}

init(
file: Data,
fileName: String,
mimeType: MIMEType.Audio,
model: ModelID,
prompt: String?,
responseFormat: String?,
temperature: Double?,
language: Language?
) {
let builder = MultipartFormDataBuilder(boundary: boundary)

builder.addDataField(
fieldName: "file",
fileName: fileName,
data: file,
mimeType: mimeType.rawValue
)

builder.addTextField(named: "model", value: model.id)

if let prompt = prompt {
builder.addTextField(named: "prompt", value: prompt)
}

if let responseFormat = responseFormat {
builder.addTextField(named: "response_format", value: responseFormat)
}

if let temperature = temperature {
builder.addTextField(named: "temperature", value: String(temperature))
}

if let language = language {
builder.addTextField(named: "language", value: language.rawValue)
}

self.body = builder.build()
}
}

49 changes: 49 additions & 0 deletions ios/OpenAIKit/Audio/CreateTranslationRequest.swift
@@ -0,0 +1,49 @@
import Foundation

struct CreateTranslationRequest: Request {
let method: HTTPMethod = .post
let path = "/v1/audio/translations"
let body: Data?
private let boundary = UUID().uuidString

var headers: HTTPHeaders {
var headers = HTTPHeaders()
headers.add(name: .contentType, value: "multipart/form-data; boundary=\(boundary)")
return headers
}

init(
file: Data,
fileName: String,
mimeType: MIMEType.Audio,
model: ModelID,
prompt: String?,
responseFormat: String?,
temperature: Double?
) {
let builder = MultipartFormDataBuilder(boundary: boundary)

builder.addDataField(
fieldName: "file",
fileName: fileName,
data: file,
mimeType: mimeType.rawValue
)

builder.addTextField(named: "model", value: model.id)

if let prompt = prompt {
builder.addTextField(named: "prompt", value: prompt)
}

if let responseFormat = responseFormat {
builder.addTextField(named: "response_format", value: responseFormat)
}

if let temperature = temperature {
builder.addTextField(named: "temperature", value: String(temperature))
}

self.body = builder.build()
}
}
22 changes: 22 additions & 0 deletions ios/OpenAIKit/Audio/Transcription.swift
@@ -0,0 +1,22 @@
//
// Transcription.swift
//
//
// Created by Joshua Galvan on 6/12/23.
//


import Foundation

/**
Audio
Learn how to turn audio into text.
Related guide: https://platform.openai.com/docs/guides/speech-to-text
*/

public struct Transcription {
public let text: String
}

extension Transcription: Codable {}
22 changes: 22 additions & 0 deletions ios/OpenAIKit/Audio/Translation.swift
@@ -0,0 +1,22 @@
//
// Translation.swift
//
//
// Created by Joshua Galvan on 6/12/23.
//


import Foundation

/**
Audio
Learn how to turn audio into text.
Related guide: https://platform.openai.com/docs/guides/speech-to-text
*/

public struct Translation {
public let text: String
}

extension Translation: Codable {}

0 comments on commit 91a054b

Please sign in to comment.