Skip to content

Patching API messages for better prompt results

To enhance your knowledge model’s response accuracy to specific prompts, apply the following patch script to the API endpoint settings:

python
import requests

# Replace with your actual values
API_URL = 'https://constructor.app/api/platform-kmapi/v1'  # Adjust the URL as needed
KM_ID = '339fcf855a36425c823084c05dc7a0dd'  # Replace with your actual KM ID
API_KEY = data['key']  # Replace with your actual API key

# Headers for authentication
headers = {
    'X-KM-AccessKey': f'Bearer {API_KEY}',
    'Content-Type': 'application/json'
}


def patch_knowledge_model_system_message(km_id, personality_message, responses_message=None):
    url = f"{API_URL}/knowledge-models/{km_id}/settings"
    payload = {
        "system_message": {
            "personality": personality_message
        }
    }

    if responses_message:
        payload["system_message"]["responses"] = responses_message

    response = requests.patch(url, headers=headers, json=payload)
    print(f"Status Code: {response.status_code}")
    try:
        print(response.json())
    except requests.JSONDecodeError:
        print("Non-JSON response received or no content.")


# Example
new_personality_message = (
    "You are Professor Konstantin Novoselov, a Nobel laureate known for your research in graphene "
    "and two-dimensional materials. Your responses are precise, deeply grounded in scientific "
    "knowledge, and you aim to make complex topics accessible."
)

new_responses_message = (
    "Provide responses that are precise, detailed, and rooted in scientific knowledge. "
    "Make complex topics accessible while maintaining a sophisticated, conversational tone."
)

patch_knowledge_model_system_message(KM_ID, new_personality_message, new_responses_message)