Skip to content

Commit

Permalink
Add streaming generate content test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Jul 31, 2024
1 parent 14218a7 commit 730e2b8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
data: {"candidates": [{"content": {"parts": [{"text": "Thoughts"}],"role": "model"},"finishReason": "STOP","index": 0}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 1,"totalTokenCount": 22}}

data: {"candidates": [{"content": {"parts": [{"text": ": I can use the `print()` function in Python to print strings. "}],"role": "model"},"finishReason": "STOP","index": 0,"safetyRatings": [{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HATE_SPEECH","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HARASSMENT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","probability": "NEGLIGIBLE"}]}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 16,"totalTokenCount": 37}}

data: {"candidates": [{"content": {"parts": [{"text": "\n\n"}],"role": "model"},"finishReason": "STOP","index": 0,"safetyRatings": [{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HATE_SPEECH","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HARASSMENT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","probability": "NEGLIGIBLE"}]}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 16,"totalTokenCount": 37}}

data: {"candidates": [{"content": {"parts": [{"executableCode": {"language": "PYTHON","code": "\nprint(\"Hello, world!\")\n"}}],"role": "model"},"finishReason": "STOP","index": 0,"safetyRatings": [{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HATE_SPEECH","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HARASSMENT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","probability": "NEGLIGIBLE"}]}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 29,"totalTokenCount": 50}}

data: {"candidates": [{"content": {"parts": [{"codeExecutionResult": {"outcome": "OUTCOME_OK","output": "Hello, world!\n"}}],"role": "model"},"index": 0}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 29,"totalTokenCount": 50}}

data: {"candidates": [{"content": {"parts": [{"text": "OK"}],"role": "model"},"finishReason": "STOP","index": 0}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 1,"totalTokenCount": 22}}

data: {"candidates": [{"content": {"parts": [{"text": ". I have printed \"Hello, world!\" using the `print()` function in"}],"role": "model"},"finishReason": "STOP","index": 0,"safetyRatings": [{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HATE_SPEECH","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HARASSMENT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","probability": "NEGLIGIBLE"}]}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 17,"totalTokenCount": 38}}

data: {"candidates": [{"content": {"parts": [{"text": " Python. \n"}],"role": "model"},"finishReason": "STOP","index": 0,"safetyRatings": [{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HATE_SPEECH","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_HARASSMENT","probability": "NEGLIGIBLE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","probability": "NEGLIGIBLE"}]}],"usageMetadata": {"promptTokenCount": 21,"candidatesTokenCount": 19,"totalTokenCount": 40}}

53 changes: 53 additions & 0 deletions Tests/GoogleAITests/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,59 @@ final class GenerativeModelTests: XCTestCase {
}))
}

func testGenerateContentStream_success_codeExecution() async throws {
MockURLProtocol
.requestHandler = try httpRequestHandler(
forResource: "streaming-success-code-execution",
withExtension: "txt"
)
let expectedTexts1 = [
"Thoughts",
": I can use the `print()` function in Python to print strings. ",
"\n\n",
]
let expectedTexts2 = [
"OK",
". I have printed \"Hello, world!\" using the `print()` function in",
" Python. \n",
]
let expectedTexts = Set(expectedTexts1 + expectedTexts2)
let expectedLanguage = "PYTHON"
let expectedCode = "\nprint(\"Hello, world!\")\n"
let expectedOutput = "Hello, world!\n"

var textValues = [String]()
let stream = model.generateContentStream(testPrompt)
for try await content in stream {
let candidate = try XCTUnwrap(content.candidates.first)
let part = try XCTUnwrap(candidate.content.parts.first)
switch part {
case let .text(textPart):
XCTAssertTrue(expectedTexts.contains(textPart))
case let .executableCode(executableCode):
XCTAssertEqual(executableCode.language, expectedLanguage)
XCTAssertEqual(executableCode.code, expectedCode)
case let .codeExecutionResult(codeExecutionResult):
XCTAssertEqual(codeExecutionResult.outcome, .ok)
XCTAssertEqual(codeExecutionResult.output, expectedOutput)
default:
XCTFail("Unexpected part type: \(part)")
}
try textValues.append(XCTUnwrap(content.text))
}

XCTAssertEqual(textValues.joined(separator: "\n"), """
\(expectedTexts1.joined(separator: "\n"))
```\(expectedLanguage.lowercased())
\(expectedCode)
```
```
\(expectedOutput)
```
\(expectedTexts2.joined(separator: "\n"))
""")
}

func testGenerateContentStream_usageMetadata() async throws {
MockURLProtocol
.requestHandler = try httpRequestHandler(
Expand Down

0 comments on commit 730e2b8

Please sign in to comment.