Skip to content

Commit d48aa1b

Browse files
Merge pull request #112 from jamesrochabrun/jroch-more-models
OpenRouter demo
2 parents 6f1a8dd + be61015 commit d48aa1b

File tree

2 files changed

+68
-42
lines changed

2 files changed

+68
-42
lines changed

Examples/SwiftOpenAIExample/SwiftOpenAIExample/OptionsListView.swift

+42-42
Original file line numberDiff line numberDiff line change
@@ -42,49 +42,49 @@ struct OptionsListView: View {
4242
var body: some View {
4343
List(options, id: \.self, selection: $selection) { option in
4444
Text(option.rawValue)
45-
.sheet(item: $selection) { selection in
46-
VStack {
47-
Text(selection.rawValue)
48-
.font(.largeTitle)
49-
.padding()
50-
switch selection {
51-
case .audio:
52-
AudioDemoView(service: openAIService)
53-
case .chat:
54-
ChatDemoView(service: openAIService)
55-
case .chatPredictedOutput:
56-
ChatPredictedOutputDemoView(service: openAIService)
57-
case .vision:
58-
ChatVisionDemoView(service: openAIService)
59-
case .embeddings:
60-
EmbeddingsDemoView(service: openAIService)
61-
case .fineTuning:
62-
FineTuningJobDemoView(service: openAIService)
63-
case .files:
64-
FilesDemoView(service: openAIService)
65-
case .images:
66-
ImagesDemoView(service: openAIService)
67-
case .localChat:
68-
LocalChatDemoView(service: openAIService)
69-
case .models:
70-
ModelsDemoView(service: openAIService)
71-
case .moderations:
72-
ModerationDemoView(service: openAIService)
73-
case .chatHistoryConversation:
74-
ChatStreamFluidConversationDemoView(service: openAIService)
75-
case .chatFunctionCall:
76-
ChatFunctionCallDemoView(service: openAIService)
77-
case .chatFunctionsCallStream:
78-
ChatFunctionsCalllStreamDemoView(service: openAIService)
79-
case .chatStructuredOutput:
80-
ChatStructuredOutputDemoView(service: openAIService)
81-
case .chatStructuredOutputTool:
82-
ChatStructureOutputToolDemoView(service: openAIService)
83-
case .configureAssistant:
84-
AssistantConfigurationDemoView(service: openAIService)
85-
}
86-
}
45+
}
46+
.sheet(item: $selection) { selection in
47+
VStack {
48+
Text(selection.rawValue)
49+
.font(.largeTitle)
50+
.padding()
51+
switch selection {
52+
case .audio:
53+
AudioDemoView(service: openAIService)
54+
case .chat:
55+
ChatDemoView(service: openAIService)
56+
case .chatPredictedOutput:
57+
ChatPredictedOutputDemoView(service: openAIService)
58+
case .vision:
59+
ChatVisionDemoView(service: openAIService)
60+
case .embeddings:
61+
EmbeddingsDemoView(service: openAIService)
62+
case .fineTuning:
63+
FineTuningJobDemoView(service: openAIService)
64+
case .files:
65+
FilesDemoView(service: openAIService)
66+
case .images:
67+
ImagesDemoView(service: openAIService)
68+
case .localChat:
69+
LocalChatDemoView(service: openAIService)
70+
case .models:
71+
ModelsDemoView(service: openAIService)
72+
case .moderations:
73+
ModerationDemoView(service: openAIService)
74+
case .chatHistoryConversation:
75+
ChatStreamFluidConversationDemoView(service: openAIService)
76+
case .chatFunctionCall:
77+
ChatFunctionCallDemoView(service: openAIService)
78+
case .chatFunctionsCallStream:
79+
ChatFunctionsCalllStreamDemoView(service: openAIService)
80+
case .chatStructuredOutput:
81+
ChatStructuredOutputDemoView(service: openAIService)
82+
case .chatStructuredOutputTool:
83+
ChatStructureOutputToolDemoView(service: openAIService)
84+
case .configureAssistant:
85+
AssistantConfigurationDemoView(service: openAIService)
8786
}
87+
}
8888
}
8989
}
9090
}

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ SwiftOpenAI supports various providers that are OpenAI-compatible, including but
104104
- [Gemini](#gemini)
105105
- [Ollama](#ollama)
106106
- [Groq](#groq)
107+
- [OpenRouter](#openRouter)
107108
- [AIProxy](#aiproxy)
108109

109110
Check OpenAIServiceFactory for convenience initializers that you can use to provide custom URLs.
@@ -3254,6 +3255,31 @@ let service = OpenAIServiceFactory.service(apiKey: apiKey, overrideBaseURL: "htt
32543255

32553256
For Supported API's using Groq visit its [documentation](https://console.groq.com/docs/openai).
32563257

3258+
## OpenRouter
3259+
3260+
[OpenRouter](https://openrouter.ai/docs/quick-start) provides an OpenAI-compatible completion API to 314 models & providers that you can call directly, or using the OpenAI SDK. Additionally, some third-party SDKs are available.
3261+
3262+
<img width="734" alt="Image" src="https://github.com/user-attachments/assets/2d658d07-0b41-4b5f-a094-ec7856f6fe98" />
3263+
3264+
```swift
3265+
3266+
// Creating the service
3267+
3268+
let apiKey = "your_api_key"
3269+
let servcie = OpenAIServiceFactory.service(apiKey: apiKey,
3270+
overrideBaseURL: "https://openrouter.ai",
3271+
proxyPath: "api",
3272+
extraHeaders: [
3273+
"HTTP-Referer": "<YOUR_SITE_URL>", // Optional. Site URL for rankings on openrouter.ai.
3274+
"X-Title": "<YOUR_SITE_NAME>" // Optional. Site title for rankings on openrouter.ai.
3275+
])
3276+
3277+
// Making a
3278+
let prompt = "What is the Manhattan project?"
3279+
let parameters = ChatCompletionParameters(messages: [.init(role: .user, content: .text(prompt))], model: .custom("deepseek/deepseek-r1:free"))
3280+
let stream = service.startStreamedChat(parameters: parameters)
3281+
```
3282+
32573283
## Gemini
32583284

32593285
<img width="982" alt="Screenshot 2024-11-12 at 10 53 43 AM" src="https://github.com/user-attachments/assets/cebc18fe-b96d-4ffe-912e-77d625249cf2">

0 commit comments

Comments
 (0)