Skip to content

turangarusso/OpenAI-Swift-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation


 _______  _______ _________
(  ___  )(  ____ )\__   __/
| (   ) || (    )|   ) (   
| (___) || (____)|   | |   
|  ___  ||  _____)   | |   
| (   ) || (         | |   
| )   ( || )      ___) (___
|/     \||/       \_______/

OpenAI Swift API

A swift rest api for OpenAI .

Key FeaturesHow To UseCreditsLicense

63aaf2cee4ecef17661a7974_OpenAI

Key Features

The OpenAI API can be applied to virtually any task that involves understanding or generating natural language or code.

  • Use all models of OpenAi
    • You need to create an accout on OpenAi and get an API key.
  • Get response and store in a struct
  • Code adaptable to every need
    • Content generation
    • Summarization
    • Classification, categorization, and sentiment analysis
    • Data extraction
    • Translation
    • Many more!
  • Integrated with swiftUI
Method Target Description
text-generation prompt Generates text based on a given prompt
sentiment-analysis text Determines the sentiment of a given text
question-answering question Provides an answer to a given question based on a given context
context
summarization text Generates a short summary of a given text
translation text Translates text from one language to another
target
language-detection text Detects the language of a given text
text-classification text Classifies a given text into one or more predefined categories
categories
named-entity-recog. text Identifies named entities in a given text
text-to-speech text Converts text into spoken audio
image-recognition image Identifies objects and scenes in an image
text-to-image prompt Generates an image based on a given prompt
semantic-search query Finds documents related to a given query
documents
chatbot message Generates responses to a given message

How To Use

You can build the project using Xcode. To get the Api key go to your account on https://beta.openai.com/account/api-keys and copy the key.

In this example i've used Davinci "text-davinci-003" model with text completions, you can change the model and use what you need.

Change the value with your OpenAi Api key

urlRequest.httpMethod = "POST"
        
urlRequest.addValue("Bearer YOUR-API-KEY", forHTTPHeaderField: "Authorization")
        
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
        

You can change and add or remove all OpenAi parameters:

  guard let url = URL(string: "https://api.openai.com/v1/completions") else {
            fatalError("Missing URL")
        }
.        
.
.
let requestData = RequestData(model: "text-davinci-003" ,prompt: "This is a test",temperature: 1, max_tokens: 50)
.
.
.
struct RequestData: Codable {
      var model: String
      var prompt: String
      var temperature: Int
      var max_tokens: Int
  }

Please read the official OpenAi API Documentation: https://beta.openai.com/docs if you need more info !

In the view you can easy call the Api using:

  .onAppear {
            network.getResponse()
        }

To get response text in the view, you can use:

Text(network.result.resultText)

The Struct must be declared with the same parameters of the API Response

If you change the Request parameters, the response may change, so you need to edit the struct!

struct TranslationResponse: Decodable {
    var id: String
    var object: String
    var created: Int
    var model: String
    var choices: [TextCompletionChoice]
    
    var resultText: String {
        choices.map(\.text).joined(separator: "\n")
    }
}

extension TranslationResponse {
    struct TextCompletionChoice: Decodable{
        var text: String
        var index: Int
//      var logprobs: String?
        var finish_reason: String
    }
}

Note

You can found more information on: https://platform.openai.com/docs/api-reference/completions/create

Credits

THIS IS NOT THE OFFICIAL API CODE

Russo Giovanni M.

OpenAI

For educational purposes

License

MIT