The Context API allows you to search and retrieve relevant information from both proprietary datasets and the web. By default, it intelligently chooses the most relevant results for your query from all available sources.
from valyu import Valyu# The SDK will automatically use VALYU_API_KEY from environmentvalyu = Valyu()response = valyu.context( query="Explain modern portfolio theory", search_type="all",# Can be "proprietary", "web", or "all" data_sources=["valyu/valyu-arxiv","valyu/valyu-wikipedia"],# Optional max_num_results=10, max_price=10, similarity_threshold=0.4, query_rewrite=True)# Access the resultsfor result in response.results:print(f"Title: {result.title}")print(f"Content: {result.content}")print(f"Source: {result.source}")print(f"Relevance Score: {result.relevance_score}")print(f"Price: ${result.price}")print(f"URL: {result.url}")
{"success":bool,# Whether the request was successful"query":str,# The original input query"error":str,# Optional: describes issues if partial response"tx_id":str,# Transaction ID for this request (useful for feedback)"results":[{"title":str,# Title of the content"url":str,# Link to the content"content":str,# Extracted text from the source"description":str,# Brief description of the content"source":str,# Which index the data is from"price":float,# Cost of accessing the content (PCM)"length":int,# Character count of the content"relevance_score":float,# Score between 0-1 indicating relevance"source_type":str,# "proprietary" or "web""data_type":str# "structured" or "unstructured"}],"results_by_source":{"proprietary":int,# Number of proprietary results"web":int# Number of web results},"total_deduction_pcm":float,# Total cost per 1000 queries"total_deduction_dollars":float,# Total cost in USD"total_characters":int# Total characters retrieved}
response = valyu.context( query="What is the capital structure?", search_type="all", max_num_results=5, similarity_threshold=0.6, query_rewrite=True, data_sources=["valyu/valyu-arxiv","https://www.valyu.network/docs"])
# First, make a context requestresponse = valyu.context( query="Explain modern portfolio theory", search_type="all", max_price=10)# Get the transaction ID from the responsetx_id = response.tx_id# Later, provide feedback on the resultsfeedback_response = valyu.feedback( tx_id=tx_id, feedback="The results were very helpful for my research", sentiment="very good")print(f"Feedback submitted: {feedback_response.success}")