I have a list of sentences that represent intents for an smart home assistant, such as "the user wants to turn on the light", each associated with an order in a machine understandable format; in this example, "light_on". I also have another list with places, such as "The user is speaking about the kitchen", again associated with (in this example) "kitchen". I will receive a sentence in natural language, such as "USER: 'turn off the lights in the kitchen and the living room", and the model (using embeddings) should return something like {"action": "light_off", "places": ["kitchen", "living_room"]}. Specify the model in a variable, in this case my preferred model is model = "jina/jina-embeddings-v2-base-es:latest".
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To achieve the desired functionality using the ollama-python library with embeddings, you can leverage the embed method to process the natural language input and match it with the predefined intents and places based on similarity scores. Below is a Python implementation that meets your requirements:
Implementation
python
from ollama import embed
from typing import List, Dict
# Define your model
model = "jina/jina-embeddings-v2-base-es:latest"
# Predefined intents and places with their respective machine-understandable formats
intents = [
{"sentence": "The user wants to turn on the light", "action": "light_on"},
{"sentence": "The user wants to turn off the light", "action": "light_off"},
{"sentence": "The user wants to dim the light", "action": "light_dim"},
# Add more intents as needed
]
places = [
{"sentence": "The user is speaking about the kitchen", "place": "kitchen"},
{"sentence": "The user is speaking about the living room", "place": "living_room"},
{"sentence": "The user is speaking about the bedroom", "place": "bedroom"},
# Add more places as needed
]
# Function to calculate similarity between embeddings
def cosine_similarity(vec1, vec2):
from numpy import dot
from numpy.linalg import norm
return dot(vec1, vec2) / (norm(vec1) * norm(vec2))
# Function to process the user query
def process_query(user_query: str) -> Dict[str, List[str]]:
# Embed the user query
user_embedding = embed(model=model, input=user_query)["embeddings"][0]
# Find the most similar intent
intent_embeddings = [
embed(model=model, input=intent["sentence"])["embeddings"][0] for intent in intents
]
best_intent_idx = max(
range(len(intent_embeddings)),
key=lambda i: cosine_similarity(user_embedding, intent_embeddings[i]),
)
best_intent = intents[best_intent_idx]["action"]
# Find the most similar places mentioned in the query
place_embeddings = [
embed(model=model, input=place["sentence"])["embeddings"][0] for place in places
]
relevant_places = [
places[i]["place"]
for i in range(len(place_embeddings))
if cosine_similarity(user_embedding, place_embeddings[i]) > 0.8
] # Adjust the threshold as needed
return {"action": best_intent, "places": relevant_places}
# Example usage
user_query = "turn off the lights in the kitchen and the living room"
result = process_query(user_query)
print(result)
Explanation of the Code
Define the Model:
The variable model specifies the embedding model to use. You have set it to "jina/jina-embeddings-v2-base-es:latest".
Predefined Intents and Places:
intents is a list of dictionaries where each dictionary maps a descriptive sentence (e.g., "The user wants to turn off the light") to a machine-readable action (e.g., "light_off").
Similarly, places maps descriptive sentences to machine-readable place names.
Embedding the Query:
The function embed from ollama generates embeddings for the user query and the predefined sentences.
Similarity Measure:
cosine_similarity is used to compute the similarity between the embedding of the user query and the embeddings of the predefined intents/places.
Matching Intents:
For intents, the code finds the sentence with the highest similarity to the user query and returns the associated action.
Matching Places:
For places, it filters out all sentences with a similarity score above a certain threshold (e.g., 0.8) and returns the associated places as a list.
Threshold:
The similarity threshold for places can be adjusted to fine-tune the behavior of the model when matching multiple places.
Example Query:
The query "turn off the lights in the kitchen and the living room" is processed to match the action "light_off" and places ["kitchen", "living_room"].
Output for the Example Query
If the predefined intents and places are as described, running the code with the input:
python
user_query = "turn off the lights in the kitchen and the living room"
result = process_query(user_query)
print(result)
Embedding multiple sentences repeatedly can be computationally expensive. If your intents and places are static, precompute their embeddings and reuse them.
Fine-Tuning:
Adjust the similarity threshold for places to better suit your use case. A lower threshold may include more results, while a higher threshold ensures more precise matches.
Model Compatibility:
Ensure that the specified model (jina/jina-embeddings-v2-base-es:latest) is compatible with ollama-python and supports embedding generation.
Extensibility:
This approach can easily be extended to include more sophisticated natural language understanding techniques, such as using templates or additional context for disambiguation.