Tutorial rápido de código: Chat con un modelo
Puede interactuar con los modelos de la Fundación en un formato conversacional utilizando la API de chat watsonx o los SDK. Puede ver distintos tipos de mensajes, como avisos del sistema, entradas del usuario y respuestas del modelo de base, incluidas preguntas y respuestas de seguimiento. Puede utilizar la API de chat para interactuar con un modelo de la fundación del mismo modo que el modo de chat en Prompt Lab.
Este tutorial muestra una interacción de chat en la que se discute la distancia entre París y Bangalore. Trabaja con Python, Node.js, o Curl.
Puedes completar este tutorial en menos de 5 minutos.
- Información detallada
- Añadir la función de chat generativo a sus aplicaciones con la API de chat
Prerrequisito
Completar las tareas previas de una sola vez.
Chatea con una modelo
Para generar texto, añada el siguiente código al archivo example.py . Sustituya {watsonx_ai_url}, {apikey}, y {project_id} por sus valores.
from ibm_watsonx_ai import APIClient
from ibm_watsonx_ai import Credentials
from ibm_watsonx_ai.foundation_models import ModelInference
credentials = Credentials(
url = "{watsonx_ai_url},
api_key = "{apikey}"
)
client = APIClient(credentials)
params = {
"time_limit": 10000,
"max_new_token": 100
}
model_id = "meta-llama/llama-3-8b-instruct"
project_id = "{project_id}"
space_id = None # optional
verify = False
model = ModelInference(
model_id=model_id,
api_client=client,
params=params,
project_id=project_id,
space_id=space_id,
verify=verify,
)
messages = [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "How far is Paris from Bangalore?"
}
]
},
{
"role": "assistant",
"content": "The distance between Paris, France, and Bangalore, India, is approximately 7,800 kilometers (4,850 miles)"
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is the flight distance?"
}
]
}
]
print(model.chat(messages=messages))
Salida de ejemplo:
{
"index": 0,
"message": {
"role": "assistant",
"content": "The flight distance between Paris, France (also known as CDG or ORY airports) and Bangalore, India is about 8,244 kilometers (5,115 miles)."
},
"finish_reason": "stop"
}
Para chatear con un modelo de fundación, añada el siguiente código al archivo example.js . Sustituya {watsonx_ai_url} y {project_id} por sus valores.
const { WatsonXAI } = require('@ibm-cloud/watsonx-ai');
process.env.IBM_CREDENTIALS_FILE = './.env'; // Your file with the apikey
const watsonxAIService = WatsonXAI.newInstance({
version: '2024-05-31',
serviceUrl: '{watsonx_ai_url}',
});
const params = {
modelId: 'meta-llama/llama-3-8b-instruct',
projectId: '{project_id}',
maxTokens: 100,
};
const messages = [
{
role: 'system',
content: 'You are a helpful assistant.',
},
{
role: 'user',
content: [
{
type: 'text',
text: 'How far is Paris from Bangalore?',
},
],
},
{
role: 'assistant',
content:
'The distance between Paris, France, and Bangalore, India, is approximately 7,800 kilometers (4,850 miles)',
},
{
role: 'user',
content: [
{
type: 'text',
text: 'What is the flight distance?',
},
],
},
];
try {
watsonxAIService
.textChat({ messages, ...params })
.then(async ({ result }) => {
console.log({ response: result.choices?.[0].message });
});
} catch (err) {
console.warn(err);
}
Salida de ejemplo:
{
"index": 0,
"message": {
"role": "assistant",
"content": "The flight distance between Paris, France (also known as CDG or ORY airports) and Bangalore, India is about 8,244 kilometers (5,115 miles)."
},
"finish_reason": "stop"
}
Para chatear con un modelo, ejecute la siguiente solicitud curl. Sustituya {token}, {watsonx_ai_url}, y {project_id} por sus valores.
curl -X POST \
-H "Authorization: Bearer {token}" \
-H "Content-Type: application/json" \
"{watsonx_ai_url}/ml/v1/text/chat?version=2024-05-31" \
--data-raw '{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "How far is Paris from Bangalore?"
}
]
},
{
"role": "assistant",
"content": "The distance between Paris, France, and Bangalore, India, is approximately 7,800 kilometers (4,850 miles)"
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is the flight distance?"
}
]
}
],
"parameters": {
"max_new_tokens": 100,
"time_limit": 10000
},
"model_id": "meta-llama/llama-3-2-3b-instruct",
"project_id": "{project_id}"
}'
Salida de ejemplo:
{
"id":"chatcmpl-d3157d1e9a4a2a60e4573a9b250c31f1",
"model_id":"meta-llama/llama-3-2-3b-instruct",
"model":"meta-llama/llama-3-2-3b-instruct",
"choices": [
{
"index":0,
"message": {
"role":"assistant",
"content":"The flight distance, also known as the straight-line distance, between Paris, France, and Bangalore, India, is approximately 7,390 kilometers (4,570 miles)."
},
"finish_reason":"stop"
}
],
"created":1741146945,
"model_version":"3.2.0",
"created_at":"2025-02-24T01:27:00.592Z",
"usage":{
"completion_tokens":36,
"prompt_tokens":89,
"total_tokens":125
},
"system":{
"warnings":[
{
"message":"This model is a Non-IBM Product governed by a third-party license that may impose use restrictions and other obligations. By using this model you agree to its terms as identified in the following URL.",
"id":"disclaimer_warning",
"more_info":"https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models.html?context=wx"
},
{
"message":"The value of 'max_tokens' for this model was set to value 1024",
"id":"unspecified_max_token",
"additional_properties":{
"limit":0,
"new_value":1024,
"parameter":"max_tokens",
"value":0
}
}
]
}
}
Was the topic helpful?
Próximos pasos
- Pruebe su solicitud con otro modelo de fundación cambiando el valor del parámetro
model_id. Ver Lista de modelos compatibles. - Prueba otro tutorial de codificación de inicio rápido.