Skip to content

Commit

Permalink
fix: parse gemini data incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 19, 2023
1 parent 0011ea1 commit 23bbb42
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions Easydict/Feature/Service/OpenAI/EZOpenAIService.m
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ - (NSString *)parseContentFromStreamData:(NSData *)data

// Convert data to string
NSString *jsonDataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"jsonDataString: %@", jsonDataString);
// NSLog(@"jsonDataString: %@", jsonDataString);

// split string to array
NSString *dataKey = @"data:";
Expand Down Expand Up @@ -513,13 +513,40 @@ - (NSString *)parseContentFromStreamData:(NSData *)data
}
}

/**
gemini-pro
{
"choices" : [
{
"delta" : {
"content" : "乌克兰可能再获一套爱国者反导系统。"
},
"finish_reason" : "stop"
}
],
"created" : 1702957216,
"id" : "chatcmpl-0ddd85aae7fe49af9444ced85875decf",
"model" : "gemini",
"object" : "chat.completion.chunk"
}
*/

// TODO: We need to optimize this code.
if (json[@"choices"]) {
NSArray *choices = json[@"choices"];
if (choices.count == 0) {
continue;
}
NSDictionary *choice = choices[0];
if (choice[@"delta"]) {
NSDictionary *delta = choice[@"delta"];
if (delta) {
if (delta[@"content"]) {
NSString *content = delta[@"content"];
// NSLog(@"content: %@", content);
[mutableString appendString:content];
}

// finish_reason is NSNull if not stop
NSString *finishReason = choice[@"finish_reason"];
if ([finishReason isKindOfClass:NSString.class] && [finishReason isEqualToString:@"stop"]) {
Expand All @@ -529,12 +556,6 @@ - (NSString *)parseContentFromStreamData:(NSData *)data
}
break;
}

NSDictionary *delta = choice[@"delta"];
if (delta[@"content"]) {
NSString *content = delta[@"content"];
[mutableString appendString:content];
}
}
}
}
Expand Down

0 comments on commit 23bbb42

Please sign in to comment.