(question)
| 189 | |
| 190 | |
| 191 | def get_rag_response(question): |
| 192 | try: |
| 193 | # Initialize TwelveLabs client |
| 194 | twelvelabs_client = TwelveLabs(api_key=TWELVELABS_API_KEY) |
| 195 | |
| 196 | # Generate embedding for the question with fashion context |
| 197 | question_with_context = f"fashion product: {question}" |
| 198 | question_embedding = twelvelabs_client.embed.create( |
| 199 | model_name="Marengo-retrieval-2.7", |
| 200 | text=question_with_context |
| 201 | ).text_embedding.segments[0].embeddings_float |
| 202 | |
| 203 | search_params = { |
| 204 | "metric_type": "COSINE", |
| 205 | "params": { |
| 206 | "nprobe": 1024, |
| 207 | "ef": 64 |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | # Search for relevant text embeddings |
| 212 | text_results = collection.search( |
| 213 | data=[question_embedding], |
| 214 | anns_field="vector", |
| 215 | param=search_params, |
| 216 | limit=2, |
| 217 | expr="embedding_type == 'text'", |
| 218 | output_fields=["metadata"] |
| 219 | ) |
| 220 | |
| 221 | # Search for relevant video segments |
| 222 | video_results = collection.search( |
| 223 | data=[question_embedding], |
| 224 | anns_field="vector", |
| 225 | param=search_params, |
| 226 | limit=3, |
| 227 | expr="embedding_type == 'video'", |
| 228 | output_fields=["metadata", "vector"] # Add vector to output fields |
| 229 | ) |
| 230 | |
| 231 | st.write(f"Retrieved {len(video_results)} video results") |
| 232 | |
| 233 | # Process text results |
| 234 | text_docs = [] |
| 235 | for hits in text_results: |
| 236 | for hit in hits: |
| 237 | metadata = hit.metadata |
| 238 | similarity = round((hit.score + 1) * 50, 2) |
| 239 | similarity = max(0, min(100, similarity)) |
| 240 | |
| 241 | text_docs.append({ |
| 242 | "title": metadata.get('title', 'Untitled'), |
| 243 | "description": metadata.get('description', 'No description available'), |
| 244 | "product_id": metadata.get('product_id', ''), |
| 245 | "video_url": metadata.get('video_url', ''), |
| 246 | "link": metadata.get('link', '#'), # Default to '#' if no link |
| 247 | "similarity": similarity, |
| 248 | "raw_score": hit.score, |
no test coverage detected