I want to use embeddings (model jina/jina-embeddings-v2-base-es:latest, but keep it in a variable so I can change it easily if necessary) to find intents for a smart home assistant. The input would be a string with a natural language sentence in spanish. Example "da la luz de cocina y salón". The output should be like this example {"action":"light_on", "rooms":["cocina", "salón"]}
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 create a solution for your smart home assistant that uses embeddings to extract intents and entities (like action and rooms) from Spanish natural language input, you can leverage the ollama-python library with the embed method. The steps below outline how to achieve this:
1. Define Your Model and Input
Keep the embedding model in a variable for flexibility, e.g., jina/jina-embeddings-v2-base-es:latest. The input will be a natural language sentence in Spanish.
2. Translate Input to Structured Output
The goal is to map natural language sentences like "da la luz de cocina y salón" into structured JSON output, such as:
Use the embedding model to generate a representation of the input sentence.
Optionally, build a lightweight classifier or heuristic-based parser to extract action and rooms.
To simplify, you can use a prompt-based approach with the ollama.chat method for structured output generation.
4. Implementation
Below is a Python implementation using the ollama-python library.
Example Code:
python
from ollama import chat
from pydantic import BaseModel
from typing import List
# Define the schema for the response
class SmartHomeIntent(BaseModel):
action: str
rooms: List[str]
def extract_intent(input_text: str, model: str = "jina/jina-embeddings-v2-base-es:latest") -> SmartHomeIntent:
"""
Extracts intent and rooms from a natural language sentence in Spanish.
Args:
input_text (str): The input sentence, e.g., "da la luz de cocina y salón".
model (str): The embedding or chat model to use.
Returns:
SmartHomeIntent: A structured response with `action` and `rooms`.
"""
# Use the chat method for structured outputs
response = chat(
model=model,
messages=[
{
"role": "user",
"content": (
f"Input: {input_text}\n"
"Extract the action and rooms in JSON format. Example:\n"
"Input: 'da la luz de cocina y salón'\n"
"Output: {\"action\": \"light_on\", \"rooms\": [\"cocina\", \"salón\"]}\n"
"Input: 'sube la temperatura de la habitación'\n"
"Output: {\"action\": \"increase_temperature\", \"rooms\": [\"habitación\"]}"
)
}
],
options={"temperature": 0}, # Deterministic responses
)
# Parse the response JSON
structured_response = SmartHomeIntent.model_validate_json(response.message.content)
return structured_response
# Example Usage
if __name__ == "__main__":
input_sentence = "da la luz de cocina y salón"
intent = extract_intent(input_sentence)
print(intent)
5. Explanation
Model Selection:
The model is stored in the model variable for easy swapping.
For example, you can replace "jina/jina-embeddings-v2-base-es:latest" with another compatible model if necessary.
Prompt Engineering:
The messages parameter includes a user prompt that provides examples of how to extract intents and rooms from input sentences.
This guides the model to generate the desired structured JSON output.
Validation:
The SmartHomeIntent class (using pydantic) ensures that the output conforms to the expected schema, making it easier to handle errors.
Deterministic Responses:
The options={"temperature": 0} parameter ensures the output is consistent and reproducible.
Predefined Entity Mapping: Use a lookup table for room names and actions to improve accuracy.
Fine-tuning: You can fine-tune the model for domain-specific intent detection.
Embedding-Based Similarity: Use embeddings to calculate cosine similarity against predefined intents if you want a non-prompt-based, classification-like approach.
This implementation provides a flexible and scalable way to extract intents and entities for your smart home assistant using the ollama-python library.