이것은 OpenAI HTTP API에 접근하기 위한 커뮤니티 유지보수 라이브러리입니다. 전체 API 문서는 여기에서 확인할 수 있습니다: https://beta.openai.com/docs
Package.swift 파일에 다음 의존성을 추가하거나 Xcode 내에서 직접 추가하여 라이브러리를 사용할 수 있습니다.
.package(url: "https://github.com/adamrushy/OpenAISwift.git", from: "1.2.0")
CocoaPods는 Cocoa 프로젝트를 위한 의존성 관리자입니다. 사용 및 설치 지침은 웹사이트를 방문하십시오. OpenAISwift를 CocoaPods를 사용하여 Xcode 프로젝트에 통합하려면, Podfile에 명시합니다:
pod 'OpenAISwift'
소스 파일을 자체 프로젝트로 복사합니다.
프로젝트에서 프레임워크를 가져옵니다:
import OpenAISwift
OpenAI API 키를 생성하고 구성에 추가합니다:
let openAI = OpenAISwift(config: OpenAISwift.Config.makeDefaultOpenAI(apiKey: MY SECRET KEY))
이 프레임워크는 Swift concurrency를 지원하며, 아래의 각 예제는 async/await 방식과 completion handler 방식 두 가지 모두를 가지고 있습니다.
입력 텍스트에 대한 예측 완료 (Predict completions for input text).
openAI.sendCompletion(with: "Hello how are you") { result in // Result<OpenAI, OpenAIError>
switch result {
case .success(let success):
...
이것은 완료 내용을 포함하는 OpenAI 객체를 반환합니다.
다른 API 매개변수도 지원됩니다:
do {
let result = try await openAI.sendCompletion(
with: "What's your favorite color?",
...
지원되는 모델의 전체 목록은 OpenAIModelType.swift를 참조하십시오. 모델에 대한 더 많은 정보는 OpenAI API Documentation을 참고하십시오.
ChatGPT (별칭 GPT-3.5) 및 GPT-4(베타 버전)를 통해 채팅 대화 응답 받기 (Get responses to chat conversations through ChatGPT (aka GPT-3.5) and GPT-4 (in beta)).
do {
let chat: [ChatMessage] = [
ChatMessage(role: .system, content: "You are a helpful assistant."),
...
모든 API 매개변수가 지원되지만, 완료되기 전에 스트리밍 메시지 내용은 제외됩니다:
do {
let chat: [ChatMessage] = [...]
let result = try await openAI.sendChat(
...
프롬프트 기반으로 이미지 생성하기 (Generate an image based on a prompt).
openAI.sendImages(with: "A 3d render of a rocket ship", numImages: 1, size: .size1024) { result in // Result<OpenAI, OpenAIError>
switch result {
case .success(let success):
...
}
프롬프트와 지침에 따라 텍스트를 수정합니다.
do {
let result = try await openAI.sendEdits(
with: "Improve the tone of this text.",
...
검열 목적으로 텍스트를 분류합니다 (자세한 내용은 OpenAI 참고).
do {
let result = try await openAI.sendModeration(
with: "Some harmful text...",
...
주어진 입력의 벡터 표현을 얻어 기계 학습 모델 및 알고리즘에서 쉽게 소비할 수 있게 합니다 (자세한 내용은 OpenAI 참고).
do {
let result = try await openAI.sendEmbeddings(
with: "The food was delicious and the waiter..."
...
이 라이브러리는 주로 재미로 만들었으며, 더 많은 엔드포인트를 추가하고 라이브러리를 더욱 탐색할 수 있습니다. PR을 제출하여 라이브러리 성장에 도움을 주십시오.
MIT License (MIT)
Copyright (c) 2022 Adam Rush
본 허가(Permission)는 본 소프트웨어 및 관련 문서 파일(