You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python实现Watson Assistant聊天机器人交互的方法咨询

Hey there! Let's break down how to get your Python script talking to Watson Assistant and make it return "result is 5" when you send x=5—you don’t even need Node.js for this flow, Python can handle the interaction directly. Here’s a step-by-step guide:

Step 1: Configure Watson Assistant to Handle Your Request

First, set up your Watson Assistant skill to recognize and respond to inputs like x=5:

  • Log into your Watson Assistant instance (in IBM Cloud) and create a new assistant, then add a dialog skill to it.
  • Create a new intent (e.g., ExtractValue) and add user input examples like x=5, x=12, my variable is x=7—this helps Watson learn to recognize your input pattern.
  • Navigate to the Dialog tab and create a new node. Set the node’s condition to #ExtractValue (so it triggers when the intent is matched).
  • For the node’s response, use Watson’s built-in text extraction to pull the number after x=. Enter this as the response text:

    result is

  • Save all your dialog changes.
Step 2: Set Up Python to Communicate with Watson

Use IBM’s official Watson Python SDK to send messages and receive responses:

  1. Install the required packages:
    pip install ibm-watson python-dotenv
    
  2. Create a Python script with the following code (replace the placeholder credentials with your own from your Watson Assistant service dashboard):
    from ibm_watson import AssistantV2
    from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
    from dotenv import load_dotenv
    import os
    
    # Load secure credentials from a .env file (never hardcode them!)
    load_dotenv()
    API_KEY = os.getenv('WATSON_API_KEY')
    ASSISTANT_ID = os.getenv('WATSON_ASSISTANT_ID')
    SERVICE_URL = os.getenv('WATSON_SERVICE_URL')
    
    # Initialize the Watson Assistant client
    authenticator = IAMAuthenticator(API_KEY)
    assistant = AssistantV2(
        version='2021-06-14',
        authenticator=authenticator
    )
    assistant.set_service_url(SERVICE_URL)
    
    # Create a session with Watson
    session_response = assistant.create_session(assistant_id=ASSISTANT_ID).get_result()
    session_id = session_response['session_id']
    
    # Send your test input: "x=5"
    message_input = {'text': 'x=5'}
    watson_response = assistant.message(
        assistant_id=ASSISTANT_ID,
        session_id=session_id,
        input=message_input
    ).get_result()
    
    # Extract and print Watson's response
    output_text = watson_response['output']['generic'][0]['text']
    print(output_text)  # Should print: "result is 5"
    
    # Clean up the session
    assistant.delete_session(assistant_id=ASSISTANT_ID, session_id=session_id)
    
  3. Create a .env file in the same folder to store your credentials:
    WATSON_API_KEY=your_actual_api_key_here
    WATSON_ASSISTANT_ID=your_actual_assistant_id_here
    WATSON_SERVICE_URL=your_actual_service_url_here
    
Step 3: Test the Flow

Run your Python script—you should see "result is 5" printed to the console. The SDK handles all the JSON formatting and API communication under the hood, so you don’t have to manually construct JSON payloads.

Key References to Deepen Your Setup

  • Watson Assistant Python SDK Docs: Covers advanced session management, message customization, and error handling.
  • Watson Dialog Expression Language: Learn more about text extraction, variables, and dynamic responses in dialog nodes (like the extract() function we used).

内容的提问来源于stack exchange,提问作者rakamakafo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:34:52