Returns:Dict[str, Any] - A dictionary containing the search results under the results key.Raises:NamespaceNotFound, InvalidInputError.Text search across one namespace
Search Example
Copy
results = client.similarity_search.query( namespaces=["my-faq-documents"], query="How long do I have to return an item?", top_k=1)print(results['results'])
chat_history = [ {"role": "user", "content": "What are your business hours?"}, {"role": "assistant", "content": "Our business hours are Monday to Friday, 9 AM to 5 PM EST."}]response = client.answer.generate( namespace="customer-support", query="What about weekends?", chat_history=chat_history, temperature=0.5, ai_model="anthropic.claude-3-sonnet-20240229-v1:0")print(f"AI Answer: {response['answer']}")print(f"Model Used: {response['model']}")print(f"Context Count: {response['contextCount']}")
from moorcheh_sdk import MoorchehClientimport timewith MoorchehClient() as client: namespace = "customer-support" # 1. Create namespace and upload support documents client.namespace.create(namespace, type="text") support_docs = [ { "id": "policy-1", "text": "Our return policy allows returns within 30 days of purchase with original receipt.", "category": "returns" }, { "id": "policy-2", "text": "We offer free shipping on orders over $50. Standard shipping takes 3-5 business days.", "category": "shipping" }, { "id": "hours-1", "text": "Our customer service is available Monday-Friday 9AM-5PM EST. We're closed on weekends.", "category": "hours" } ] client.documents.upload(namespace, support_docs) print("Documents uploaded, waiting for processing...") time.sleep(5) # 2. Perform searches print("\n=== SEARCH RESULTS ===") search_results = client.similarity_search.query( namespaces=[namespace], query="return policy", top_k=2 ) for result in search_results['results']: print(f"Score: {result['score']:.3f} | ID: {result['id']}") print(f"Text: {result['text'][:80]}...") print() # 3. Get AI-generated answers print("\n=== AI ANSWERS ===") questions = [ "What is your return policy?", "How long does shipping take?", "Are you open on weekends?" ] for question in questions: response = client.answer.generate( namespace=namespace, query=question, top_k=1 ) print(f"Q: {question}") print(f"A: {response['answer']}") print()
{ 'answer': 'Generated answer text...', 'model': 'anthropic.claude-3-sonnet-20240229-v1:0', 'contextCount': 3, # Number of documents used as context 'query': 'Original user query'}