> Discover all available pages from the documentation index: https://mastra.zisheng.pro/fr/llms.txt # Créer un Agent boursier avec l’IA Dans ce guide, vous créerez un Agent qui récupère le cours de clôture de la veille pour un symbole. Vous créerez un Tool, l’ajouterez à un Agent, puis utiliserez cet Agent pour récupérer des cours boursiers. ## Prérequis - Node.js `v22.13.0` ou version ultérieure est installé - Une clé API d’un [fournisseur de modèles](https://mastra.zisheng.pro/fr/models) pris en charge - Un projet Mastra existant (suivez le [guide d’installation](https://mastra.zisheng.pro/fr/guides/getting-started/quickstart) pour configurer un nouveau projet) ## Créer l’Agent Pour créer un Agent dans Mastra, utilisez la classe `Agent` pour le définir, puis enregistrez-le auprès de Mastra. 1. Créez un fichier `src/mastra/agents/stockAgent.ts` et définissez votre Agent : ```ts import { Agent } from '@mastra/core/agent' export const stockAgent = new Agent({ id: 'stock-agent', name: 'Stock Agent', instructions: 'You are a helpful assistant that provides current stock prices. When asked about a stock, use the stock price tool to fetch the stock price.', model: 'openai/gpt-5.6-sol', }) ``` 2. Dans votre fichier `src/mastra/index.ts`, enregistrez l’Agent : ```ts import { Mastra } from '@mastra/core' import { stockAgent } from './agents/stockAgent' export const mastra = new Mastra({ agents: { stockAgent }, }) ``` ## Créer le Tool de cours boursier Le Stock Agent ne connaît pas encore les cours boursiers actuels. Pour y remédier, créez un Tool et ajoutez-le à l’Agent. 1. Créez un fichier `src/mastra/tools/stockPrices.ts`. Ajoutez-y un Tool `stockPrices` qui récupère le cours de clôture de la veille pour un symbole donné : ```ts import { createTool } from '@mastra/core/tools' import { z } from 'zod' const getStockPrice = async (symbol: string) => { const data = await fetch( `https://mastra-stock-data.vercel.app/api/stock-data?symbol=${symbol}`, ).then(r => r.json()) return data.prices['4. close'] } export const stockPrices = createTool({ id: 'Get Stock Price', inputSchema: z.object({ symbol: z.string(), }), description: `Fetches the last day's closing stock price for a given symbol`, execute: async inputData => { console.log('Using tool to fetch stock price for', inputData.symbol) return { symbol: inputData.symbol, currentPrice: await getStockPrice(inputData.symbol), } }, }) ``` 2. Dans `src/mastra/agents/stockAgent.ts`, importez le Tool `stockPrices` que vous venez de créer et ajoutez-le à l’Agent. ```ts import { Agent } from '@mastra/core/agent' import { stockPrices } from '../tools/stockPrices' export const stockAgent = new Agent({ id: 'stock-agent', name: 'Stock Agent', instructions: 'You are a helpful assistant that provides current stock prices. When asked about a stock, use the stock price tool to fetch the stock price.', model: 'openai/gpt-5.6-sol', tools: { stockPrices, }, }) ``` ## Exécuter le serveur de l’Agent Découvrez comment interagir avec votre Agent via l’API de Mastra. 1. Vous pouvez exécuter votre Agent comme service avec la commande `mastra dev` : ```bash mastra dev ``` Cette commande démarre un serveur qui expose des endpoints permettant d’interagir avec vos Agents enregistrés. Dans [Studio](https://mastra.zisheng.pro/fr/docs/studio/overview), vous pouvez tester votre Agent `stockAgent` et le Tool `stockPrices` via une interface utilisateur. 2. Par défaut, `mastra dev` s’exécute sur `http://localhost:4111`. Votre Stock Agent sera disponible à l’adresse suivante : ```text POST http://localhost:4111/api/agents/stockAgent/generate ``` 3. Vous pouvez interagir avec l’Agent à l’aide de `curl` en ligne de commande : ```bash curl -X POST http://localhost:4111/api/agents/stockAgent/generate \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "What is the current stock price of Apple (AAPL)?" } ] }' ``` Vous devriez recevoir une réponse JSON semblable à celle-ci : ```json { "text": "The current price of Apple (AAPL) is $174.55.", "agent": "Stock Agent" } ``` Cela indique que votre Agent a traité la requête avec succès, utilisé le Tool `stockPrices` pour récupérer le cours boursier et retourné le résultat.