-
Notifications
You must be signed in to change notification settings - Fork 682
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
#435: Add metadata support (read/write) to pinecone embedded store #955
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import com.google.protobuf.Struct; | ||
import com.google.protobuf.Value; | ||
import dev.langchain4j.data.document.Metadata; | ||
import dev.langchain4j.data.embedding.Embedding; | ||
import dev.langchain4j.data.segment.TextSegment; | ||
import dev.langchain4j.store.embedding.CosineSimilarity; | ||
|
@@ -13,10 +14,10 @@ | |
import io.pinecone.PineconeConnection; | ||
import io.pinecone.PineconeConnectionConfig; | ||
import io.pinecone.proto.*; | ||
import io.pinecone.proto.Vector; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import static dev.langchain4j.internal.Utils.randomUUID; | ||
import static java.util.Collections.emptyList; | ||
|
@@ -41,13 +42,13 @@ public class PineconeEmbeddingStore implements EmbeddingStore<TextSegment> { | |
/** | ||
* Creates an instance of PineconeEmbeddingStore. | ||
* | ||
* @param apiKey The Pinecone API key. | ||
* @param environment The environment (e.g., "northamerica-northeast1-gcp"). | ||
* @param projectId The ID of the project (e.g., "19a129b"). This is <b>not</b> a project name. | ||
* The ID can be found in the Pinecone URL: https://app.pinecone.io/organizations/.../projects/...:{projectId}/indexes. | ||
* @param index The name of the index (e.g., "test"). | ||
* @param nameSpace (Optional) Namespace. If not provided, "default" will be used. | ||
* @param metadataTextKey (Optional) The key to find the text in the metadata. If not provided, "text_segment" will be used. | ||
* @param apiKey The Pinecone API key. | ||
* @param environment The environment (e.g., "northamerica-northeast1-gcp"). | ||
* @param projectId The ID of the project (e.g., "19a129b"). This is <b>not</b> a project name. | ||
* The ID can be found in the Pinecone URL: <a href="https://app.pinecone.io/organizations/.../projects/">...</a>...:{projectId}/indexes. | ||
* @param index The name of the index (e.g., "test"). | ||
* @param nameSpace (Optional) Namespace. If not provided, "default" will be used. | ||
* @param metadataTextKey (Optional) The key to find the text in the metadata. If not provided, "text_segment" will be used. | ||
*/ | ||
public PineconeEmbeddingStore(String apiKey, | ||
String environment, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that |
||
|
@@ -136,12 +137,16 @@ private void addAllInternal(List<String> ids, List<Embedding> embeddings, List<T | |
vectorBuilder.setMetadata(Struct.newBuilder() | ||
.putFields(metadataTextKey, Value.newBuilder() | ||
.setStringValue(textSegments.get(i).text()) | ||
.build())); | ||
.build()) | ||
.putAllFields(textSegments.get(i).metadata().asMap().entrySet() | ||
.stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, e -> Value.newBuilder().setStringValue(e.getValue()).build())))); | ||
} | ||
|
||
upsertRequestBuilder.addVectors(vectorBuilder.build()); | ||
} | ||
|
||
//noinspection ResultOfMethodCallIgnored | ||
connection.getBlockingStub().upsert(upsertRequestBuilder.build()); | ||
} | ||
|
||
|
@@ -184,22 +189,47 @@ public List<EmbeddingMatch<TextSegment>> findRelevant(Embedding referenceEmbeddi | |
return matches; | ||
} | ||
|
||
|
||
private EmbeddingMatch<TextSegment> toEmbeddingMatch(Vector vector, Embedding referenceEmbedding) { | ||
Value textSegmentValue = vector.getMetadata() | ||
Struct metadataStruct = vector.getMetadata(); | ||
|
||
Value textSegmentValue = metadataStruct | ||
.getFieldsMap() | ||
.get(metadataTextKey); | ||
|
||
boolean filterOutMetadataTextKey = true; | ||
Map<String, String> metadataMap = structToMap(metadataStruct, filterOutMetadataTextKey); | ||
Metadata metadata = Metadata.from(metadataMap); | ||
|
||
Embedding embedding = Embedding.from(vector.getValuesList()); | ||
double cosineSimilarity = CosineSimilarity.between(embedding, referenceEmbedding); | ||
|
||
return new EmbeddingMatch<>( | ||
RelevanceScore.fromCosineSimilarity(cosineSimilarity), | ||
vector.getId(), | ||
embedding, | ||
textSegmentValue == null ? null : TextSegment.from(textSegmentValue.getStringValue()) | ||
textSegmentValue == null ? null : TextSegment.from(textSegmentValue.getStringValue(), metadata) | ||
); | ||
} | ||
|
||
private Map<String, String> structToMap(Struct struct, boolean filterOutMetadataTextKey) { | ||
Map<String, String> result = new HashMap<>(); | ||
Map<String, Value> fields = struct.getFieldsMap(); | ||
|
||
for (Map.Entry<String, Value> entry : fields.entrySet()) { | ||
if (filterOutMetadataTextKey && isMetadataTextKey(entry.getKey())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when retreiving I am filtering out metadataTextKey from metadata, otherwise tests were failing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We store original embedded text under The problem can happen in such case: One option is to prepend all metadata keys with
in Pinecone's metadata. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about just making sure your not going to have a key collision and throwing an exception if it happens? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sfotex makes sense! |
||
continue; | ||
} | ||
result.put(entry.getKey(), entry.getValue().getStringValue()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private boolean isMetadataTextKey(String key) { | ||
return metadataTextKey.equals(key); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
@@ -231,7 +261,7 @@ public Builder environment(String environment) { | |
|
||
/** | ||
* @param projectId The ID of the project (e.g., "19a129b"). This is <b>not</b> a project name. | ||
* The ID can be found in the Pinecone URL: https://app.pinecone.io/organizations/.../projects/...:{projectId}/indexes. | ||
* The ID can be found in the Pinecone URL: <a href="https://app.pinecone.io/organizations/.../projects/">...</a>...:{projectId}/indexes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
*/ | ||
public Builder projectId(String projectId) { | ||
this.projectId = projectId; | ||
|
@@ -257,6 +287,7 @@ public Builder nameSpace(String nameSpace) { | |
/** | ||
* @param metadataTextKey (Optional) The key to find the text in the metadata. If not provided, "text_segment" will be used. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public Builder metadataTextKey(String metadataTextKey) { | ||
this.metadataTextKey = metadataTextKey; | ||
return this; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed to add this function to be able to use Awaitility with pinecone client to wait for records to be retrievable.
Pinecone is eventually consistent, so records are/may not be available just after write
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff! What do you think about changing existing
awaitUntilPersisted()
instead of introducing new overloaded method? I guess the implementation can also be moved to theEmbeddingStoreIT
so that all implementations can benefit from using awaitility?