Skip to content
mistral-core-workflow-b logo

Mistral Core Workflow B

mistral-core-workflow-b

Execute Mistral AI secondary workflows: Embeddings and Function Calling. Use when implementing semantic search, RAG applications, or tool-augmented LLM interactions. Trigger with phrases like "mistral embeddings", "mistral function calling", "mistral tools", "mistral RAG", "mistral semantic search".

SKILL.md

Full skill instructions

Mistral AI Core Workflow B: Embeddings & Function Calling

Overview

Secondary workflows for Mistral AI: Text embeddings for semantic search and function calling for tool use.

Prerequisites

  • Completed mistral-install-auth setup
  • Familiarity with mistral-core-workflow-a
  • Valid API credentials configured

Embeddings

Step 1: Generate Text Embeddings

import Mistral from '@mistralai/​mistralai';

const client = new Mistral({
  apiKey: process.env.MISTRAL_API_KEY,
});

async function getEmbedding(text: string): Promise<number[]> {
  const response = await client.embeddings.create({
    model: 'mistral-embed',
    inputs: [text],
  });

  return response.data[0].embedding;
}

// Usage
const embedding = await getEmbedding('Machine learning is fascinating.');
console.log(`Embedding dimensions: ${embedding.length}`); // 1024

Step 2: Batch Embeddings

async function getBatchEmbeddings(texts: string[]): Promise<number[][]> {
  const response = await client.embeddings.create({
    model: 'mistral-embed',
    inputs: texts,
  });

  return response.data.map(d => d.embedding);
}

// Usage
const documents = [
  'Python is a programming language.',
  'JavaScript runs in browsers.',
  'Rust is memory safe.',
];
const embeddings = await getBatchEmbeddings(documents);

Step 3: Semantic Search Implementation

function cosineSimilarity(a: number[], b: number[]): number {
  let dotProduct = 0;
  let normA = 0;
  let normB = 0;

  for (let i = 0; i < a.length; i++) {
    dotProduct += a[i] * b[i];
    normA += a[i] * a[i];
    normB += b[i] * b[i];
  }

  return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}

interface Document {
  id: string;
  text: string;
  embedding?: number[];
}

class SemanticSearch {
  private documents: Document[] = [];
  private client: Mistral;

  constructor() {
    this.client = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });
  }

  async indexDocuments(docs: Omit<Document, 'embedding'>[]): Promise<void> {
    const texts = docs.map(d => d.text);
    const response = await this.client.embeddings.create({
      model: 'mistral-embed',
      inputs: texts,
    });

    this.documents = docs.map((doc, i) => ({
      ...doc,
      embedding: response.data[i].embedding,
    }));
  }

  async search(query: string, topK = 5): Promise<Array<Document & { score: number }>> {
    const queryEmbedding = await this.getEmbedding(query);

    const results = this.documents
      .map(doc => ({
        ...doc,
        score: cosineSimilarity(queryEmbedding, doc.embedding!),
      }))
      .sort((a, b) => b.score - a.score)
      .slice(0, topK);

    return results;
  }

  private async getEmbedding(text: string): Promise<number[]> {
    const response = await this.client.embeddings.create({
      model: 'mistral-embed',
      inputs: [text],
    });
    return response.data[0].embedding;
  }
}

// Usage
const search = new SemanticSearch();
await search.indexDocuments([
  { id: '1', text: 'How to bake chocolate chip cookies' },
  { id: '2', text: 'Machine learning fundamentals' },
  { id: '3', text: 'Best practices for React development' },
]);

const results = await search.search('AI tutorials', 2);
// Returns documents sorted by semantic similarity

Function Calling

Step 4: Define Tools

interface Tool {
  type: 'function';
  function: {
    name: string;
    description: string;
    parameters: {
      type: 'object';
      properties: Record<string, { type: string; description: string }>;
      required: string[];
    };
  };
}

const tools: Tool[] = [
  {
    type: 'function',
    function: {
      name: 'get_weather',
      description: 'Get the current weather for a location',
      parameters: {
        type: 'object',
        properties: {
          location: {
            type: 'string',
            description: 'City and country, e.g., "London, UK"',
          },
          unit: {
            type: 'string',
            description: 'Temperature unit: "celsius" or "fahrenheit"',
          },
        },
        required: ['location'],
      },
    },
  },
  {
    type: 'function',
    function: {
      name: 'search_web',
      description: 'Search the web for information',
      parameters: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'The search query',
          },
        },
        required: ['query'],
      },
    },
  },
];

Step 5: Implement Function Calling Loop

import Mistral from '@mistralai/​mistralai';

const client = new Mistral({
  apiKey: process.env.MISTRAL_API_KEY,
});

// Tool implementations
const toolFunctions: Record<string, (args: any) => Promise<string>> = {
  get_weather: async ({ location, unit = 'celsius' }) => {
    // Simulated weather API call
    return JSON.stringify({
      location,
      temperature: unit === 'celsius' ? 22 : 72,
      unit,
      condition: 'sunny',
    });
  },
  search_web: async ({ query }) => {
    // Simulated web search
    return JSON.stringify({
      results: [
        { title: `Result for: ${query}`, url: 'https://example.com' },
      ],
    });
  },
};

async function chatWithTools(userMessage: string): Promise<string> {
  const messages: any[] = [
    { role: 'user', content: userMessage },
  ];

  while (true) {
    const response = await client.chat.complete({
      model: 'mistral-large-latest',
      messages,
      tools,
      toolChoice: 'auto',
    });

    const choice = response.choices?.[0];
    const assistantMessage = choice?.message;

    if (!assistantMessage) {
      throw new Error('No response from model');
    }

    // Add assistant message to conversation
    messages.push(assistantMessage);

    // Check if model wants to call tools
    const toolCalls = assistantMessage.toolCalls;

    if (!toolCalls || toolCalls.length === 0) {
      // No tool calls, return final response
      return assistantMessage.content ?? '';
    }

    // Execute tool calls
    for (const toolCall of toolCalls) {
      const functionName = toolCall.function.name;
      const functionArgs = JSON.parse(toolCall.function.arguments);

      console.log(`Calling tool: ${functionName}`, functionArgs);

      const toolFn = toolFunctions[functionName];
      if (!toolFn) {
        throw new Error(`Unknown tool: ${functionName}`);
      }

      const result = await toolFn(functionArgs);

      // Add tool result to conversation
      messages.push({
        role: 'tool',
        name: functionName,
        content: result,
        toolCallId: toolCall.id,
      });
    }
  }
}

// Usage
const answer = await chatWithTools('What is the weather in Paris?');
console.log(answer);
// "The current weather in Paris is sunny with a temperature of 22°C."

Step 6: RAG (Retrieval-Augmented Generation)

class RAGChat {
  private search: SemanticSearch;
  private client: Mistral;

  constructor() {
    this.search = new SemanticSearch();
    this.client = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });
  }

  async indexKnowledge(documents: { id: string; text: string }[]): Promise<void> {
    await this.search.indexDocuments(documents);
  }

  async chat(userQuery: string): Promise<string> {
    // Retrieve relevant documents
    const relevantDocs = await this.search.search(userQuery, 3);

    // Build context from retrieved documents
    const context = relevantDocs
      .map(doc => `[Source ${doc.id}]: ${doc.text}`)
      .join('\n\n');

    // Generate response with context
    const response = await this.client.chat.complete({
      model: 'mistral-small-latest',
      messages: [
        {
          role: 'system',
          content: `Answer questions based on the provided context. If the answer is not in the context, say so.

Context:
${context}`,
        },
        { role: 'user', content: userQuery },
      ],
    });

    return response.choices?.[0]?.message?.content ?? '';
  }
}

// Usage
const rag = new RAGChat();
await rag.indexKnowledge([
  { id: '1', text: 'Mistral AI was founded in 2023 in Paris, France.' },
  { id: '2', text: 'The mistral-embed model produces 1024-dimensional vectors.' },
]);

const answer = await rag.chat('When was Mistral AI founded?');

Output

  • Text embeddings (1024 dimensions)
  • Semantic search implementation
  • Function calling with tool execution
  • RAG chat with retrieval

Error Handling

AspectWorkflow AWorkflow B
Use CaseChat/​Text GenerationEmbeddings/​Tools
ModelsAll chat modelsmistral-embed + large
ComplexityLowerMedium-Higher
LatencyDepends on tokensBatching helps

Examples

Python Embeddings

import os
from mistralai import Mistral

client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))

def get_embeddings(texts: list[str]) -> list[list[float]]:
    response = client.embeddings.create(
        model="mistral-embed",
        inputs=texts,
    )
    return [d.embedding for d in response.data]

Python Function Calling

import json
from mistralai import Mistral

client = Mistral(api_key=os.environ.get("MISTRAL_API_KEY"))

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City name"}
                },
                "required": ["location"]
            }
        }
    }
]

response = client.chat.complete(
    model="mistral-large-latest",
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
    tools=tools,
    tool_choice="auto"
)

Resources

Next Steps

For common errors, see mistral-common-errors.

More skills from Dicklesworthstone

environment-setup-guide logo
Dicklesworthstone/pi_agent_rust

environment-setup-guide

Guide developers through setting up development environments with proper tools, dependencies, and configurations

1.8K 0
View
exa-policy-guardrails logo
Dicklesworthstone/pi_agent_rust

exa-policy-guardrails

Implement Exa lint rules, policy enforcement, and automated guardrails. Use when setting up code quality rules for Exa integrations, implementing pre-commit hooks, or configuring CI policy checks for Exa best practices. Trigger with phrases like "exa policy", "exa lint", "exa guardrails", "exa be...

1.8K 0
View
documenso-ci-integration logo
Dicklesworthstone/pi_agent_rust

documenso-ci-integration

Configure CI/CD pipelines for Documenso integrations. Use when setting up automated testing, deployment pipelines, or continuous integration for Documenso projects. Trigger with phrases like "documenso CI", "documenso GitHub Actions", "documenso pipeline", "documenso automated testing".

1.8K 0
View
openrouter-model-availability logo
Dicklesworthstone/pi_agent_rust

openrouter-model-availability

Build check model availability and implement fallback chains. Use when building resilient systems or handling model outages. Trigger with phrases like 'openrouter availability', 'openrouter fallback', 'openrouter model down', 'openrouter health check'.

1.8K 0
View
langfuse-enterprise-rbac logo
Dicklesworthstone/pi_agent_rust

langfuse-enterprise-rbac

Configure Langfuse enterprise organization management and access control. Use when implementing team access controls, configuring organization settings, or setting up role-based permissions for Langfuse projects. Trigger with phrases like "langfuse RBAC", "langfuse teams", "langfuse organization"...

1.8K 0
View
cursor-compliance-audit logo
Dicklesworthstone/pi_agent_rust

cursor-compliance-audit

Execute compliance and security auditing for Cursor usage. Triggers on "cursor compliance", "cursor audit", "cursor security review", "cursor soc2", "cursor gdpr". Use when analyzing or auditing cursor compliance audit. Trigger with phrases like "cursor compliance audit", "cursor audit", "cursor".

1.8K 0
View
apollo-ci-integration logo
Dicklesworthstone/pi_agent_rust

apollo-ci-integration

Configure Apollo.io CI/CD integration. Use when setting up automated testing, continuous integration, or deployment pipelines for Apollo integrations. Trigger with phrases like "apollo ci", "apollo github actions", "apollo pipeline", "apollo ci/cd", "apollo automated tests".

1.8K 0
View
python-script logo
Dicklesworthstone/pi_agent_rust

python-script

Create robust Python automation with full logging and safety checks. Use when tasks need complex data processing, authenticated API work, conditional file operations, or error handling beyond simple shell commands.

1.8K 0
View
posthog-multi-env-setup logo
Dicklesworthstone/pi_agent_rust

posthog-multi-env-setup

Configure PostHog across development, staging, and production environments. Use when setting up multi-environment deployments, configuring per-environment secrets, or implementing environment-specific PostHog configurations. Trigger with phrases like "posthog environments", "posthog staging", "po...

1.8K 0
View
ai-agents-architect logo
Dicklesworthstone/pi_agent_rust

ai-agents-architect

Expert in designing and building autonomous AI agents. Masters tool use, memory systems, planning strategies, and multi-agent orchestration. Use when: build agent, AI agent, autonomous agent, tool use, function calling.

1.8K 0
View
insecure-deserialization-checker logo
Dicklesworthstone/pi_agent_rust

insecure-deserialization-checker

Validate insecure deserialization checker operations. Auto-activating skill for Security Fundamentals. Triggers on: insecure deserialization checker, insecure deserialization checker Part of the Security Fundamentals skill category. Use when working with insecure deserialization checker functiona...

1.8K 0
View
path-traversal-finder logo
Dicklesworthstone/pi_agent_rust

path-traversal-finder

Manage path traversal finder operations. Auto-activating skill for Security Fundamentals. Triggers on: path traversal finder, path traversal finder Part of the Security Fundamentals skill category. Use when working with path traversal finder functionality. Trigger with phrases like "path traversa...

1.8K 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View