Skip to content

Commit

Permalink
feat(llms): Support seed and system_fingerprint in OpenAI wrapper (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Nov 7, 2023
1 parent 33ebe74 commit c31b679
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/langchain_openai/lib/src/llms/models/mappers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extension CreateCompletionResponseMapper on CreateCompletionResponse {
'id': id,
'created': created,
'model': model,
'systemFingerprint': systemFingerprint,
},
streaming: streaming,
);
Expand Down
12 changes: 12 additions & 0 deletions packages/langchain_openai/lib/src/llms/openai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class OpenAI extends BaseLLM<OpenAIOptions> {
this.maxTokens = 256,
this.n = 1,
this.presencePenalty = 0,
this.seed,
this.suffix,
this.temperature = 1,
this.topP = 1,
Expand Down Expand Up @@ -181,6 +182,16 @@ class OpenAI extends BaseLLM<OpenAIOptions> {
/// See https://platform.openai.com/docs/api-reference/completions/create#completions-create-presence_penalty
final double presencePenalty;

/// If specified, our system will make a best effort to sample
/// deterministically, such that repeated requests with the same seed and
/// parameters should return the same result.
///
/// Determinism is not guaranteed, and you should refer to the
/// `system_fingerprint` response parameter to monitor changes in the backend.
///
/// See https://platform.openai.com/docs/api-reference/completions/create#completions-create-seed
final int? seed;

/// The suffix that comes after a completion of inserted text.
///
/// See https://platform.openai.com/docs/api-reference/completions/create#completions-create-suffix
Expand Down Expand Up @@ -245,6 +256,7 @@ class OpenAI extends BaseLLM<OpenAIOptions> {
maxTokens: maxTokens,
n: n,
presencePenalty: presencePenalty,
seed: seed,
stop: options?.stop != null
? CompletionStop.arrayString(options!.stop!)
: null,
Expand Down
23 changes: 23 additions & 0 deletions packages/langchain_openai/test/llms/openai_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,28 @@ void main() {
expect(count, greaterThan(1));
expect(content, contains('123456789'));
});

test('Test response seed', () async {
final prompt = PromptValue.string('How are you?');
final llm = OpenAI(
apiKey: openaiApiKey,
temperature: 0,
seed: 9999,
);

final res1 = await llm.invoke(prompt);
expect(res1.generations, hasLength(1));
final generation1 = res1.generations.first;

final res2 = await llm.invoke(prompt);
expect(res2.generations, hasLength(1));
final generation2 = res2.generations.first;

expect(
res1.modelOutput?['systemFingerprint'],
res2.modelOutput?['systemFingerprint'],
);
expect(generation1.output, generation2.output);
});
});
}

0 comments on commit c31b679

Please sign in to comment.