Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ErrorDetailsView in the Chat Sample to handle invalidAPIKey #94

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 63 additions & 42 deletions Examples/GenerativeAISample/ChatSample/Views/ErrorDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ struct ErrorDetailsView: View {
SafetyRatingsSection(ratings: ratings)
}

case GenerateContentError.invalidAPIKey:
Section("Error Type") {
Text("Invalid API Key")
}

Section("Details") {
SubtitleFormRow(title: "Error description", value: error.localizedDescription)
SubtitleMarkdownFormRow(
title: "Help",
value: """
Please provide a valid value for `API_KEY` in the `GenerativeAI-Info.plist` file.
"""
)
}

default:
Section("Error Type") {
Text("Some other error")
Expand All @@ -158,49 +173,55 @@ struct ErrorDetailsView: View {
}
}

#Preview {
NavigationView {
let _ = GenerateContentError.promptBlocked(
response: GenerateContentResponse(candidates: [
CandidateResponse(content: ModelContent(role: "model", [
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
SafetyRating(category: .dangerousContent, probability: .high),
SafetyRating(category: .harassment, probability: .low),
SafetyRating(category: .hateSpeech, probability: .low),
SafetyRating(category: .sexuallyExplicit, probability: .low),
],
finishReason: FinishReason.other,
citationMetadata: nil),
#Preview("Response Stopped Early") {
let error = GenerateContentError.responseStoppedEarly(
reason: .maxTokens,
response: GenerateContentResponse(candidates: [
CandidateResponse(content: ModelContent(role: "model", [
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
SafetyRating(category: .dangerousContent, probability: .high),
SafetyRating(category: .harassment, probability: .low),
SafetyRating(category: .hateSpeech, probability: .low),
SafetyRating(category: .sexuallyExplicit, probability: .low),
],
promptFeedback: nil)
)

let errorFinishedEarly = GenerateContentError.responseStoppedEarly(
reason: .maxTokens,
response: GenerateContentResponse(candidates: [
CandidateResponse(content: ModelContent(role: "model", [
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
SafetyRating(category: .dangerousContent, probability: .high),
SafetyRating(category: .harassment, probability: .low),
SafetyRating(category: .hateSpeech, probability: .low),
SafetyRating(category: .sexuallyExplicit, probability: .low),
],
finishReason: FinishReason.maxTokens,
citationMetadata: nil),
finishReason: FinishReason.maxTokens,
citationMetadata: nil),
],
promptFeedback: nil)
)

return ErrorDetailsView(error: error)
}

#Preview("Prompt Blocked") {
let error = GenerateContentError.promptBlocked(
response: GenerateContentResponse(candidates: [
CandidateResponse(content: ModelContent(role: "model", [
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
SafetyRating(category: .dangerousContent, probability: .high),
SafetyRating(category: .harassment, probability: .low),
SafetyRating(category: .hateSpeech, probability: .low),
SafetyRating(category: .sexuallyExplicit, probability: .low),
],
promptFeedback: nil)
)
finishReason: FinishReason.other,
citationMetadata: nil),
],
promptFeedback: nil)
)

ErrorDetailsView(error: errorFinishedEarly)
}
return ErrorDetailsView(error: error)
}

#Preview("Invalid API Key") {
ErrorDetailsView(error: GenerateContentError.invalidAPIKey)
}
Loading