From d87220d03ca426c4305398c01d9c362d1f7ba038 Mon Sep 17 00:00:00 2001 From: Milan Cermak Date: Mon, 6 Jan 2025 13:55:55 +0100 Subject: [PATCH] fix: anthropic documents in prompt --- rig-core/CHANGELOG.md | 1 + rig-core/src/providers/anthropic/completion.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/rig-core/CHANGELOG.md b/rig-core/CHANGELOG.md index 08059ef42..7a99ca38d 100644 --- a/rig-core/CHANGELOG.md +++ b/rig-core/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - *(openai)* Make integration more general (#156) +- *(anthropic)* Properly format context documents in prompt (#183) ### Other diff --git a/rig-core/src/providers/anthropic/completion.rs b/rig-core/src/providers/anthropic/completion.rs index 56c00bed0..5b7a1c578 100644 --- a/rig-core/src/providers/anthropic/completion.rs +++ b/rig-core/src/providers/anthropic/completion.rs @@ -197,7 +197,20 @@ impl completion::CompletionModel for CompletionModel { // specific requirements of each provider. For now, we just manually check while // building the request as a raw JSON document. - let prompt_with_context = completion_request.prompt_with_context(); + let prompt_with_context = if completion_request.documents.is_empty() { + completion_request.prompt.clone() + } else { + let documents = completion_request.documents.into_iter().enumerate().map(|(i, doc)| { + format!( + "{}{}", + i, doc.id, doc.text + ) + }).collect::>().join("\n"); + format!( + "\n{documents}\n{}", + completion_request.prompt + ) + }; // Check if max_tokens is set, required for Anthropic let max_tokens = if let Some(tokens) = completion_request.max_tokens {