Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Mar 22, 2024
1 parent 43e8ba2 commit 3f60bf4
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 26 deletions.
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,60 @@

The definitive, open-source Swift framework for interfacing with generative AI.

# 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](https://github.com/PreternaturalAI/AI.git).
4. Choose the version you'd like to install.
5. Click `Add Package`.

# Usage

```swift
import AI
```

Chat completions:

```swift
let llm: any LLMRequestHandling = OpenAI.APIClient(apiKey: "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

let messages: [AbstractLLM.ChatMessage] = [
AbstractLLM.ChatMessage(
role: .system,
body: "You are an extremely intelligent assistant."
),
AbstractLLM.ChatMessage(
role: .user,
body: "Sup?"
)
]

let result = try await llm.complete(
messages,
model: OpenAI.Model.chat(.gpt_4)
)

print(result) // "Hello! How can I assist you today?"
```

# Roadmap

- [x] OpenAI
- [x] Anthropic
- [x] Mistral
- [x] Ollama
- [ ] Perplexity
- [ ] Groq

# Acknowledgements

- [similarity-search-kit](https://github.com/ZachNagengast/similarity-search-kit)
- [Tiktoken](https://github.com/aespinilla/Tiktoken)

# License
# License

This package is licensed under the MIT License.

This package is licensed under the MIT License.
1 change: 1 addition & 0 deletions Sources/AI/module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
@_exported import CoreMI
@_exported import LargeLanguageModels
@_exported import OpenAI
@_exported import SwallowMacrosClient
3 changes: 2 additions & 1 deletion Sources/Anthropic/module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
// Copyright (c) Vatsal Manot
//

import Swift
@_exported import LargeLanguageModels
@_exported import SwallowMacrosClient
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ extension AbstractLLM {
self.role = role
self.content = content
}

public init(
role: ChatRole,
body: PromptLiteral
) {
self.id = nil
self.role = role
self.content = body
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ extension LLMRequestHandling {
return try await complete(prompt: prompt)
}

public func complete(
messages: [AbstractLLM.ChatMessage],
model: some _MLModelIdentifierConvertible
) async throws -> AbstractLLM.ChatCompletion {
let prompt = AbstractLLM.ChatPrompt(
messages: messages,
context: try withMutableScope(PromptContextValues.current) { context in
context.completionType = .chat
context.modelIdentifier = try .one(model.__conversion())
}
)

return try await complete(prompt: prompt)
}

public func complete(
_ messages: [AbstractLLM.ChatMessage]
) async throws -> AbstractLLM.ChatCompletion {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mistral/module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// Copyright (c) Vatsal Manot
//


import Foundation
@_exported import LargeLanguageModels
@_exported import SwallowMacrosClient
1 change: 1 addition & 0 deletions Sources/Ollama/module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// Copyright (c) Vatsal Manot
//

@_exported import LargeLanguageModels
@_exported import Swallow
@_exported import SwallowMacrosClient
1 change: 1 addition & 0 deletions Sources/OpenAI/module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
// Copyright (c) Vatsal Manot
//

@_exported import LargeLanguageModels
@_exported import SwallowMacrosClient
1 change: 1 addition & 0 deletions Sources/Perplexity/module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
// Copyright (c) Vatsal Manot
//

@_exported import LargeLanguageModels
import Swift
40 changes: 20 additions & 20 deletions Tests/OpenAI/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (c) Vatsal Manot
//

import LargeLanguageModels

@testable import OpenAI

import XCTest
Expand Down Expand Up @@ -35,28 +37,26 @@ She no went to the market.
_ = result
}

func testChatCompletions() async throws {
let result = try await client.createChatCompletion(
messages: [
OpenAI.ChatMessage(
role: .system,
body: "You are an extremely intelligent assistant."
),
OpenAI.ChatMessage(
role: .user,
body: "Sup?"
),
OpenAI.ChatMessage(
role: .assistant,
body: "I'm coming up with a list of things! Here you go:"
),

],
model: .chat(.gpt_3_5_turbo),
parameters: .init()
func testChatCompletions() async throws {
let llm: any LLMRequestHandling = OpenAI.APIClient(apiKey: "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

let messages: [AbstractLLM.ChatMessage] = [
AbstractLLM.ChatMessage(
role: .system,
body: "You are an extremely intelligent assistant."
),
AbstractLLM.ChatMessage(
role: .user,
body: "Sup?"
)
]

let result = try await llm.complete(
messages,
model: OpenAI.Model.chat(.gpt_4)
)

print(result.choices)
print(result) // "Hello! How can I assist you today?"
}

func testGPTVisionTurbo() async throws {
Expand Down

0 comments on commit 3f60bf4

Please sign in to comment.