Skip to content

Commit 8bd5e9c

Browse files
committed
Add AI Server Chat test
1 parent 85304f1 commit 8bd5e9c

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

Tests/ServiceStackTests/OllamaTests.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,26 @@ import Foundation
1010
import ServiceStack
1111

1212
final class OllamaTests : @unchecked Sendable {
13-
var client: JsonServiceClient!
1413

15-
init() async throws {
16-
client = JsonServiceClient(baseUrl: "https://supermicro.pvq.app")
14+
@Test func Can_call_AiServer_Chat() async throws {
15+
let client = JsonServiceClient(baseUrl: "https://openai.servicestack.net")
16+
client.bearerToken = ProcessInfo.processInfo.environment["AI_SERVER_API_KEY"]
17+
let request = OpenAiChatCompletion()
18+
request.model = "llama3.1:8b"
19+
let msg = OpenAiMessage()
20+
msg.role = "user"
21+
msg.content = "What's the capital of France?"
22+
request.messages = [msg]
23+
request.max_tokens = 50
24+
25+
let result = try await client.sendAsync(request)
26+
27+
#expect(result.choices!.count == 1)
28+
#expect(result.choices![0].message?.content?.contains("Paris") == true)
1729
}
1830

19-
@Test func Can_serialize_Empty_Option() async throws {
31+
@Test func Can_call_Ollama_Chat() async throws {
32+
let client = JsonServiceClient(baseUrl: "https://supermicro.pvq.app")
2033
let request = OpenAiChatCompletion()
2134
// request.model = "mixtral:8x22b"
2235
request.model = "llama3.1:8b"
@@ -29,8 +42,8 @@ final class OllamaTests : @unchecked Sendable {
2942
let result:OpenAiChatResponse = try await client.postAsync(
3043
"/v1/chat/completions", request:request)
3144

32-
#expect(result.choices.count == 1)
33-
#expect(result.choices[0].message?.content?.contains("Paris") == true)
45+
#expect(result.choices!.count == 1)
46+
#expect(result.choices![0].message?.content?.contains("Paris") == true)
3447
}
3548

36-
}
49+
}

Tests/ServiceStackTests/OpenAiDtos.dtos.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Options:
2-
Date: 2024-11-21 10:25:50
2+
Date: 2024-11-23 07:25:40
33
SwiftVersion: 6.0
44
Version: 8.41
55
Tip: To override a DTO option, remove "//" prefix before updating
@@ -15,7 +15,7 @@ IncludeTypes: OpenAiChatCompletion.*,ToolCall
1515
//AddResponseStatus: False
1616
//AddImplicitVersion:
1717
//AddDescriptionAsComments: True
18-
//InitializeCollections: True
18+
//InitializeCollections: False
1919
//TreatTypesAsStrings:
2020
//DefaultImports: Foundation,ServiceStack
2121
*/
@@ -82,7 +82,7 @@ public class OpenAiChatResponse : Codable
8282
* A list of chat completion choices. Can be more than one if n is greater than 1.
8383
*/
8484
// @DataMember(Name="choices")
85-
public var choices:[Choice] = []
85+
public var choices:[Choice]?
8686

8787
/**
8888
* The Unix timestamp (in seconds) of when the chat completion was created.
@@ -148,7 +148,7 @@ public class OpenAiMessage : Codable
148148
* The tool calls generated by the model, such as function calls.
149149
*/
150150
// @DataMember(Name="tool_calls")
151-
public var tool_calls:[ToolCall] = []
151+
public var tool_calls:[ToolCall]?
152152

153153
/**
154154
* Tool call that this message is responding to.
@@ -193,7 +193,7 @@ public class OpenAiChat : Codable
193193
* A list of messages comprising the conversation so far.
194194
*/
195195
// @DataMember(Name="messages")
196-
public var messages:[OpenAiMessage] = []
196+
public var messages:[OpenAiMessage]?
197197

198198
/**
199199
* ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API
@@ -211,7 +211,7 @@ public class OpenAiChat : Codable
211211
* Modify the likelihood of specified tokens appearing in the completion.
212212
*/
213213
// @DataMember(Name="logit_bias")
214-
public var logit_bias:[Int:Int] = [:]
214+
public var logit_bias:[Int:Int]?
215215

216216
/**
217217
* Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message.
@@ -259,7 +259,7 @@ public class OpenAiChat : Codable
259259
* Up to 4 sequences where the API will stop generating further tokens.
260260
*/
261261
// @DataMember(Name="stop")
262-
public var stop:[String] = []
262+
public var stop:[String]?
263263

264264
/**
265265
* If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a `data: [DONE]` message.
@@ -283,7 +283,7 @@ public class OpenAiChat : Codable
283283
* A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.
284284
*/
285285
// @DataMember(Name="tools")
286-
public var tools:[OpenAiTools] = []
286+
public var tools:[OpenAiTools]?
287287

288288
/**
289289
* A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
@@ -395,7 +395,7 @@ public class ChoiceMessage : Codable
395395
* The tool calls generated by the model, such as function calls.
396396
*/
397397
// @DataMember(Name="tool_calls")
398-
public var tool_calls:[ToolCall] = []
398+
public var tool_calls:[ToolCall]?
399399

400400
/**
401401
* The role of the author of this message.

0 commit comments

Comments
 (0)