Code Monkey home page Code Monkey logo

polyai's Introduction

PolyAI

dragon

iOS 15+ MIT license swift-version swiftui-version xcode-version swift-package-manager

An open-source Swift package that simplifies LLM message completions, inspired by liteLLM and adapted for Swift developers, following Swift conventions.

Table of Contents

Description

Call different LLM APIs using the OpenAI format; currently supporting OpenAI and Anthropic, with more models, including Gemini, coming soon.

Installation

Swift Package Manager

  1. Open your Swift project in Xcode.
  2. Go to File -> Add Package Dependency.
  3. In the search bar, enter this URL.
  4. Choose the version you'd like to install.
  5. Click Add Package.

Important

โš ๏ธ Please take precautions to keep your API keys secure.

Remember that your API keys are a secret! Do not share it with others or expose it in any client-side code (browsers, apps). Production requests must be routed through your backend server where your API keys can be securely loaded from an environment variable or key management service.

Usage

To interface with different LLMs, you need only to supply the corresponding LLM configuration and adjust the parameters accordingly.

First, import the PolyAI package:

import PolyAI

Then, define the LLM configurations. Currently, OpenAI and Anthropic are supported:

let openAIConfiguration: LLMConfiguration = .openAI(.api(key: "your_openai_api_key_here"))
let anthropicConfiguration: LLMConfiguration = .anthropic(apiKey: "your_anthropic_api_key_here")
let configurations = [openAIConfiguration, anthropicConfiguration]

With the configurations set, initialize the service:

let service = PolyAIServiceFactory.serviceWith(configurations)

Now, you have access to both the OpenAI and Anthropic APIs in a single package, with Gemini coming soon! ๐Ÿš€

Message

To send a message using OpenAI:

let prompt = "How are you today?"
let parameters: LLMParameter = .openAI(model: .gpt4turbo, messages: [.init(role: .user, content: prompt)])
let stream = try await service.streamMessage(parameters)

To interact with Anthropic instead, all you need to do is change just one line of code! ๐Ÿ”ฅ

let prompt = "How are you today?"
let parameters: LLMParameter = .anthropic(model: .claude3Sonnet, messages: [.init(role: .user, content: prompt)], maxTokens: 1024)
let stream = try await service.streamMessage(parameters)

OpenAI Azure

To access the OpenAI API via Azure, you can use the following configuration setup.

let azureConfiguration: LLMConfiguration = .openAI(.azure(configuration: .init(resourceName: "YOUR_RESOURCE_NAME", openAIAPIKey: .apiKey("YOUR_API_KEY"), apiVersion: "THE_API_VERSION")))

More information can be found here.

OpenAI AIProxy

To access the OpenAI API via AIProxy, use the following configuration setup.

let aiProxyConfiguration: LLMConfiguration = .openAI(.aiProxy(aiproxyPartialKey: "hardcode_partial_key_here", aiproxyDeviceCheckBypass: "hardcode_device_check_bypass_here"))

More information can be found here.

Collaboration

Open a PR for any proposed change pointing it to main branch.

polyai's People

Contributors

jamesrochabrun avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.