First run will take slightly longer as it has to download the embedding model.
import jsonimport osfrom superlinked import framework as slclassProduct(sl.Schema):id: sl.IdField description: sl.String rating: sl.Integerproduct =Product()description_space = sl.TextSimilaritySpace( text=product.description, model="Alibaba-NLP/gte-large-en-v1.5")rating_maximizer_space = sl.NumberSpace( number=product.rating, min_value=1, max_value=5, mode=sl.Mode.MAXIMUM)index = sl.Index([description_space, rating_maximizer_space], fields=[product.rating])# Fill this with your API key - this will drive param extractionopenai_config = sl.OpenAIClientConfig( api_key=os.environ["OPEN_AI_API_KEY"], model="gpt-4o")# It is possible now to add descriptions to a `Param` to aid the parsing of information from natural language queries.text_similar_param = sl.Param("query_text", description="The text in the user's query that refers to product descriptions.",)# Define your query using dynamic parameters for query text and weights.# we will have our LLM fill them based on our natural language queryquery = ( sl.Query( index, weights={ description_space: sl.Param("description_weight"), rating_maximizer_space: sl.Param("rating_maximizer_weight"), }, ).find(product).similar( description_space, text_similar_param, sl.Param("description_similar_clause_weight") ).limit(sl.Param("limit")).with_natural_query(sl.Param("natural_query"), openai_config))# Run the app.source = sl.InMemorySource(product)executor = sl.InMemoryExecutor(sources=[source], indices=[index])app = executor.run()# Download dataset.data = [{"id":1,"description":"Budget toothbrush in black color.","rating":1},{"id":2,"description":"High-end toothbrush created with no compromises.","rating":5},{"id":3,"description":"A toothbrush created for the smart 21st century man.","rating":3},]# Ingest data to the framework.source.put(data)result = app.query(query, natural_query="best toothbrushes", limit=1)# Examine the extracted parameters from your queryprint(json.dumps(result.knn_params, indent=2))# The result is the 5 star rated productresult.to_pandas()