opik icon indicating copy to clipboard operation
opik copied to clipboard

[OPIK-2982] [P SDK] [BE] [FE] ChatPrompt support

Open alexkuzmik opened this issue 2 months ago • 44 comments

Details

Implemented ChatPrompt feature. Added it to:

  • Python SDK: API objects, integrated into existing prompt-related features and APIs, added new client functions to create/get/search chat prompts.
  • BE: added new filterable column to prompt database - template_structure: "text" or "chat")
  • FE: prompt library (view, create, update + Python code snippets), prompt playground (import/export).

https://pr-3987.dev.comet.com/

Demo script for new SDK API:

"""
Example: Chat Prompt Management with Opik SDK

This example demonstrates the new chat prompt functionality including:
- Creating chat prompts with multi-role messages
- Searching for prompts (both string and chat)
- Formatting prompts with variables
"""

import opik

# Initialize Opik client
client = opik.Opik()

# ============================================================================
# 1. Create a chat prompt with multiple roles
# ============================================================================
print("Creating chat prompt...")

chat_prompt = client.create_chat_prompt(  # can also be created via opik.ChatPrompt(...)
    name="customer-support-agent",
    messages=[
        {
            "role": "system",
            "content": "You are a helpful customer support agent. Be friendly and professional and answer the customer's question.",
        },
        {"role": "user", "content": "Answer me: {{customer_question}}"},
    ],
)

print(f"✓ Created: {chat_prompt.name} (commit: {chat_prompt.commit[:8]})")

# ============================================================================
# 2. Search for prompts (returns both string and chat prompts)
# ============================================================================
print("\nSearching for prompts...")

# Get all prompts
all_prompts = client.search_prompts()
print(f"✓ Found {len(all_prompts)} total prompts")

# Filter by type using OQL
chat_prompts = client.search_prompts(filter_string='template_structure = "chat"')
string_prompts = client.search_prompts(filter_string='template_structure = "text"')

print(f"  - {len(chat_prompts)} chat prompts")
print(f"  - {len(string_prompts)} string prompts")

# ============================================================================
# 3. Retrieve and format a chat prompt
# ============================================================================
print("\nRetrieving and formatting prompt...")

retrieved = client.get_chat_prompt(name="customer-support-agent")

# Format with variables
formatted_messages = retrieved.format(
    variables={"customer_question": "How do I reset my password?"}
)

print(f"✓ Formatted {len(formatted_messages)} messages:")
for msg in formatted_messages:
    print(f"  [{msg['role']}]: {msg['content'][:50]}...")

# ============================================================================
# 4. Type-safe prompt handling
# ============================================================================
print("\nType-safe prompt handling...")

# search_prompts() returns Union[Prompt, ChatPrompt]
for prompt in all_prompts[:3]:  # Show first 3
    if isinstance(prompt, opik.ChatPrompt):
        print(f"  • {prompt.name} (chat, {len(prompt.template)} messages)")
    elif isinstance(prompt, opik.Prompt):
        print(f"  • {prompt.name} (string)")

print("\n✅ Example completed!")

https://github.com/user-attachments/assets/dce53a8e-2175-4755-a4dc-7f34f30dd372 image

image image image image

Change checklist

  • [X] User facing
  • [ ] Documentation update

Issues

  • Resolves #
  • OPIK-2982

Testing

Added unit tests and e2e tests to the SDK. Tested FE manually. Added new tests to prompt resource in the BE.

Documentation

Will be added in a separate PR.

alexkuzmik avatar Nov 07 '25 16:11 alexkuzmik

📋 PR Linter Failed

Incomplete Details Section. The ## Details section cannot be empty.


Incomplete Issues Section. You must reference at least one GitHub issue (#xxxx), Jira ticket (OPIK-xxxx), (CUST-xxxx) or DEV ticket (DEV-xxxx) under the ## Issues section.

github-actions[bot] avatar Nov 07 '25 16:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-b903024f-e561-4dd4-b526-306d6632409f.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 10 '25 11:11 github-actions[bot]

📋 PR Linter Failed

Incomplete Details Section. The ## Details section cannot be empty.


Incomplete Issues Section. You must reference at least one GitHub issue (#xxxx), Jira ticket (OPIK-xxxx), (CUST-xxxx) or DEV ticket (DEV-xxxx) under the ## Issues section.

github-actions[bot] avatar Nov 10 '25 11:11 github-actions[bot]

📋 PR Linter Failed

Incomplete Details Section. The ## Details section cannot be empty.


Incomplete Issues Section. You must reference at least one GitHub issue (#xxxx), Jira ticket (OPIK-xxxx), (CUST-xxxx) or DEV ticket (DEV-xxxx) under the ## Issues section.

github-actions[bot] avatar Nov 10 '25 12:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik

The following broken links where found:

Page: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/changelog ❌ Broken link: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

Page: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/changelog/ ❌ Broken link: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

Page: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/changelog/2025/10/21 ❌ Broken link: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

Page: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/changelog/2025/10/21/ ❌ Broken link: https://opik-preview-103214e3-b51f-4bf6-bf34-3853a8971bc7.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

github-actions[bot] avatar Nov 10 '25 12:11 github-actions[bot]

📋 PR Linter Failed

Incomplete Details Section. The ## Details section cannot be empty.


Incomplete Issues Section. You must reference at least one GitHub issue (#xxxx), Jira ticket (OPIK-xxxx), (CUST-xxxx) or DEV ticket (DEV-xxxx) under the ## Issues section.

github-actions[bot] avatar Nov 10 '25 14:11 github-actions[bot]

SDK Unit Tests Results

1 014 tests   1 009 ✅  37s ⏱️     1 suites      0 💤     1 files        5 ❌

For more details on these failures, see this check.

Results for commit 42ba13b7.

:recycle: This comment has been updated with latest results.

github-actions[bot] avatar Nov 10 '25 14:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-ebfa0b91-b516-44cf-b7c6-30f6d95553fc.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 10 '25 14:11 github-actions[bot]

📋 PR Linter Failed

Incomplete Details Section. The ## Details section cannot be empty.


Incomplete Issues Section. You must reference at least one GitHub issue (#xxxx), Jira ticket (OPIK-xxxx), (CUST-xxxx) or DEV ticket (DEV-xxxx) under the ## Issues section.

github-actions[bot] avatar Nov 10 '25 15:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik

The following broken links where found:

Page: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/changelog ❌ Broken link: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

Page: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization ❌ Broken link: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization/ (404)

Page: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization/ ❌ Broken link: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization/ (404)

Page: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/changelog/ ❌ Broken link: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

Page: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/changelog/2025/10/21 ❌ Broken link: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

Page: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/changelog/2025/10/21/ ❌ Broken link: https://opik-preview-e42db50f-3a6d-49ac-9204-589697b770ac.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization (404)

github-actions[bot] avatar Nov 10 '25 15:11 github-actions[bot]

📋 PR Linter Failed

Incomplete Details Section. The ## Details section cannot be empty.


Incomplete Issues Section. You must reference at least one GitHub issue (#xxxx), Jira ticket (OPIK-xxxx), (CUST-xxxx) or DEV ticket (DEV-xxxx) under the ## Issues section.

github-actions[bot] avatar Nov 10 '25 15:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-e54dafc8-d8d2-4ef6-8913-835bd81a08f6.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 10 '25 15:11 github-actions[bot]

📋 PR Linter Failed

Incomplete Details Section. The ## Details section cannot be empty.


Incomplete Issues Section. You must reference at least one GitHub issue (#xxxx), Jira ticket (OPIK-xxxx), (CUST-xxxx) or DEV ticket (DEV-xxxx) under the ## Issues section.

github-actions[bot] avatar Nov 10 '25 15:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-d9e78834-784d-4748-8aff-1f766f43d569.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 10 '25 15:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik

The following broken links where found:

Page: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/changelog ❌ Broken link: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization (404)

Page: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization ❌ Broken link: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization/ (404)

Page: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/changelog/ ❌ Broken link: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization (404)

Page: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization/ ❌ Broken link: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/multi-metric-optimization/ (404)

Page: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/changelog/2025/10/3 ❌ Broken link: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization (404)

Page: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/changelog/2025/10/3/ ❌ Broken link: https://opik-preview-b4bab304-5141-426c-af47-ef922c6c20ae.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization (404)

Page: ❌ Broken link: {} ()

Page: ❌ Broken link: ] ()

Page: ❌ Broken link: {} ()

Page: ❌ Broken link: ] ()

github-actions[bot] avatar Nov 10 '25 18:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-42abdb08-7391-4885-ada8-70267d01fe5f.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 10 '25 18:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-23974da6-5328-4c7d-a53c-79fa317b3dfa.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 10 '25 18:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-3fcb5611-a6d3-4017-9ff4-b5fb889da2a5.docs.buildwithfern.com/docs/opik

The following broken links where found:

Page: https://opik-preview-3fcb5611-a6d3-4017-9ff4-b5fb889da2a5.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization ❌ Broken link: https://opik-preview-3fcb5611-a6d3-4017-9ff4-b5fb889da2a5.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ (404)

Page: https://opik-preview-3fcb5611-a6d3-4017-9ff4-b5fb889da2a5.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ ❌ Broken link: https://opik-preview-3fcb5611-a6d3-4017-9ff4-b5fb889da2a5.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ (404)

github-actions[bot] avatar Nov 12 '25 11:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-bcb2bb6b-9b05-4a0d-b912-1b9d4cf29c32.docs.buildwithfern.com/docs/opik

The following broken links where found:

Page: ❌ Broken link: {} ()

Page: ❌ Broken link: ] ()

Page: ❌ Broken link: {} ()

Page: ❌ Broken link: ] ()

Page: https://opik-preview-bcb2bb6b-9b05-4a0d-b912-1b9d4cf29c32.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization ❌ Broken link: https://opik-preview-bcb2bb6b-9b05-4a0d-b912-1b9d4cf29c32.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ (404)

Page: https://opik-preview-bcb2bb6b-9b05-4a0d-b912-1b9d4cf29c32.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ ❌ Broken link: https://opik-preview-bcb2bb6b-9b05-4a0d-b912-1b9d4cf29c32.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ (404)

github-actions[bot] avatar Nov 12 '25 12:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-ce19fd48-fc11-417f-b924-9de347f3b355.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 12 '25 12:11 github-actions[bot]

🔄 Test environment deployment started

Building images for PR #3987...

You can monitor the build progress here.

github-actions[bot] avatar Nov 14 '25 16:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-65129462-1407-400d-b310-43137ae4aff1.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 14 '25 16:11 github-actions[bot]

Test environment is now available!

Access Information

  • URL: https://pr-3987.dev.comet.com
  • Cluster: comet-ml-development
  • Namespace: pr-3987
  • Version: 1.9.15.3987-merge-421
  • Application logs: View in Grafana

The deployment has completed successfully and the version has been verified.

CometActions avatar Nov 14 '25 16:11 CometActions

🌿 Preview your docs: https://opik-preview-b5c4b480-cf55-4871-89dd-5e60f407cd51.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 17 '25 10:11 github-actions[bot]

The test env is still broken. Do not review.

alexkuzmik avatar Nov 17 '25 10:11 alexkuzmik

🌿 Preview your docs: https://opik-preview-c9c208db-cdf4-4408-9e51-b148227d1ba0.docs.buildwithfern.com/docs/opik

The following broken links where found:

Page: https://opik-preview-c9c208db-cdf4-4408-9e51-b148227d1ba0.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization ❌ Broken link: https://opik-preview-c9c208db-cdf4-4408-9e51-b148227d1ba0.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ (404)

Page: https://opik-preview-c9c208db-cdf4-4408-9e51-b148227d1ba0.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ ❌ Broken link: https://opik-preview-c9c208db-cdf4-4408-9e51-b148227d1ba0.docs.buildwithfern.com/docs/opik/agent_optimization/algorithms/tool_optimization/ (404)

github-actions[bot] avatar Nov 17 '25 12:11 github-actions[bot]

🌿 Preview your docs: https://opik-preview-cb5e1ee2-a06d-41bd-af91-508fe8d5e41a.docs.buildwithfern.com/docs/opik

No broken links found

github-actions[bot] avatar Nov 17 '25 16:11 github-actions[bot]

🔄 Test environment deployment started

Building images for PR #3987...

You can monitor the build progress here.

github-actions[bot] avatar Nov 18 '25 17:11 github-actions[bot]

🔄 Test environment deployment started

Building images for PR #3987...

You can monitor the build progress here.

github-actions[bot] avatar Nov 18 '25 18:11 github-actions[bot]

Test environment is now available!

Access Information

  • URL: https://pr-3987.dev.comet.com
  • Cluster: comet-ml-development
  • Namespace: pr-3987
  • Version: 1.9.18-3987-merge-447
  • Application logs: View in Grafana

The deployment has completed successfully and the version has been verified.

CometActions avatar Nov 18 '25 18:11 CometActions