Description
I’d like to provide a few sentences to explain why a result might be useful based on the query. I see multiple approaches with Cohere keys. Can you offer advice based on my code below (I’m very new to Weaviate).
- Weaviate Server Version: Serverless
- Deployment Method: web client running in localhost
- Multi Node? Number of Running Nodes: na
- Client Language and Version: English
- Multitenancy?: na
Any additional Information
Here is my code snippet running in my web client. I was trying two different approaches, one via an additionalFields generate request and the other approach was to use the .withGenerate command.
let additionalFields = `_additional { score explainScore
generate(
singleResult: {
prompt: """
In 3 sentences, explain in plain English why this result was selected for the query: '${searchQuery}'.
Use the text, title, and description as context as well as the collectionTitle and collectionDescription to better understand how this episode fits into the larger collection.
"""
}
) {
singleResult
error
}
}`;
const response = await weaviateClientRef.current.graphql
.get()
.withClassName(collection_name)
.withHybrid({ query: searchQuery, targetVectors: ["text_vector"] })
.withGenerate({
groupedTask: "In 3 sentences, explain in plain English why this result was selected for the query.",
})
.withLimit(1)
.withFields('start end type url text title slug contentId description image date bibleProjectUrl collectionTitle collectionDescription collectionContentId show ' + additionalFields)
.do();
Am I close or is this really not the right approach?
At present I’m just getting 1 result but that can scale later.
Hi @Dave_Fisher
Since you’re on Serverless in our cloud, it’s always great to open tickets via support@weaviate.io, it moves faster in our support system. It’s also helpful to share your approach in the community, as many have experience and can give useful input.
In general:
Your code is on the right track for both, just decide whether you want per-object output or a grouped explanation, and go with the one that fits.
Best regards,
Mohamed Shahin
Weaviate Support Engineer
(Ireland, UTC±00:00/+01:00)
Thanks @mohamed_shahin. I’ll do that in the future. Perhaps I’ll open a ticket for this request soon as the feature is developed. As an update here, I got it working using singleResult and the .withGenerate syntax… but, of course, the moment I got it working was the same moment I realized that I should be doing something different. :). I realized I actually need to display the results quickly with just a search showing the description text, then generate explanations as a lazy second pass. If you are willing I’ll explain my new plan to see if you like it:
I display podcast and video results to users. Each episode has many records (we chunk the transcripts) and it’s common for one episode to have many hits, so singleResult is silly since it might give 10+ explanations for the same episode if 10+ records were found within the same episode. Plus it shows nothing while generating results. So my new plan is to show the results instantly with descriptions only, then make an Ajax call to my own server for the second pass to get explanations to display to the user about why that result was selected.itll be a huge win once it works since the description is generic. The concept is simple but the details get complicated. So how is that Ajax call best implemented?.. Let me know your thoughts to these high levels questions if you are willing.
Is it necessary to go to my server or can I do this all client side with weaviate calls? Should I batch 5 episode explanations at a time or do each individually (worried about costs)? Should I use the Weaviate Agent ask feature? Should I do 1 episode at a time when a user request it or just automatically do all results (or just tip 5 results automatically the others on request only)? Should I pass the whole record or just the record unique id, it seems silly to pass around the whole record, but might be easiest. Should I even do it via Weaviate or just generate text using an OpenAi/ Cohere model directly? So many details to resolve. Thanks for any advice!
Your plan to display search results instantly with descriptions, then generate explanations as a second, lazy pass is good approach.
You can make generative queries directly from the client. Batching is recommended to improve efficiency, especially if you’re using a paid LLM provider. I think this will help with some of your concerns →
If your generative prompt only needs certain fields (e.g., description, summary), you can pass just the unique IDs to your backend, which can then fetch the necessary data from Weaviate before making the generative call. This is more efficient than passing full records through the client.
Using Weaviate’s generate module is recommended if you want to keep your workflow unified and leverage Weaviate’s search and context management.
Thanks @Mohamed_Shahin that looks like a really interesting demo that matches very closely to what I’d wanting to do. I’ll dive in and ask questions once I’ve had time to process. Thanks!
1 Like