Skip to content

Commit

Permalink
OPIK-626 PromptVersion new fields documentation and SDK (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisTkachenko authored Dec 19, 2024
1 parent f83916d commit 5906283
Show file tree
Hide file tree
Showing 35 changed files with 193 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public record PromptVersion(
@JsonView({PromptVersion.View.Public.class, Prompt.View.Detail.class,
PromptVersion.View.Detail.class}) @NotBlank String template,
@Json @JsonView({PromptVersion.View.Public.class, Prompt.View.Detail.class,
PromptVersion.View.Detail.class}) @Nullable JsonNode metadata,
PromptVersion.View.Detail.class}) JsonNode metadata,
@JsonView({PromptVersion.View.Public.class, Prompt.View.Detail.class,
PromptVersion.View.Detail.class}) @Nullable String changeDescription,
PromptVersion.View.Detail.class}) String changeDescription,
@JsonView({Prompt.View.Detail.class,
PromptVersion.View.Detail.class}) @Schema(accessMode = Schema.AccessMode.READ_ONLY) @Nullable Set<String> variables,
@JsonView({Prompt.View.Detail.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

@ImplementedBy(PromptServiceImpl.class)
public interface PromptService {
Prompt create(Prompt prompt);
Prompt create(Prompt promptRequest);

PromptPage find(String name, int page, int size);

Expand Down Expand Up @@ -75,13 +75,13 @@ class PromptServiceImpl implements PromptService {
private final @NonNull TransactionTemplate transactionTemplate;

@Override
public Prompt create(@NonNull Prompt prompt) {
public Prompt create(@NonNull Prompt promptRequest) {

String workspaceId = requestContext.get().getWorkspaceId();
String userName = requestContext.get().getUserName();

var newPrompt = prompt.toBuilder()
.id(prompt.id() == null ? idGenerator.generateId() : prompt.id())
var newPrompt = promptRequest.toBuilder()
.id(promptRequest.id() == null ? idGenerator.generateId() : promptRequest.id())
.createdBy(userName)
.lastUpdatedBy(userName)
.build();
Expand All @@ -94,18 +94,18 @@ public Prompt create(@NonNull Prompt prompt) {
createdPrompt.name(),
workspaceId);

if (!StringUtils.isEmpty(prompt.template())) {
if (!StringUtils.isEmpty(promptRequest.template())) {
EntityConstraintHandler
.handle(() -> createPromptVersionFromPromptRequest(createdPrompt, workspaceId, prompt))
.handle(() -> createPromptVersionFromPromptRequest(createdPrompt, workspaceId, promptRequest))
.withRetry(3, this::newVersionConflict);
}

return createdPrompt;
}

private PromptVersion createPromptVersionFromPromptRequest(Prompt createdPrompt,
String workspaceId,
Prompt promptPayload) {
String workspaceId,
Prompt promptRequest) {
log.info("Creating prompt version for prompt id '{}'", createdPrompt.id());

var createdVersion = transactionTemplate.inTransaction(WRITE, handle -> {
Expand All @@ -116,9 +116,9 @@ private PromptVersion createPromptVersionFromPromptRequest(Prompt createdPrompt,
.id(versionId)
.promptId(createdPrompt.id())
.commit(CommitUtils.getCommit(versionId))
.template(promptPayload.template())
.metadata(promptPayload.metadata())
.changeDescription(promptPayload.changeDescription())
.template(promptRequest.template())
.metadata(promptRequest.metadata())
.changeDescription(promptRequest.changeDescription())
.createdBy(createdPrompt.createdBy())
.build();

Expand Down
22 changes: 22 additions & 0 deletions apps/opik-documentation/documentation/rest_api/opik.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4075,6 +4075,10 @@ components:
template:
pattern: (?s)^\s*(\S.*\S|\S)\s*$
type: string
metadata:
$ref: '#/components/schemas/JsonNode'
change_description:
type: string
created_at:
type: string
format: date-time
Expand Down Expand Up @@ -4115,6 +4119,10 @@ components:
\ be 8 characters long"
template:
type: string
metadata:
$ref: '#/components/schemas/JsonNode'
change_description:
type: string
variables:
uniqueItems: true
type: array
Expand Down Expand Up @@ -4146,6 +4154,12 @@ components:
template:
pattern: (?s)^\s*(\S.*\S|\S)\s*$
type: string
metadata:
$ref: '#/components/schemas/JsonNode_Write'
change_description:
type: string
JsonNode_Detail:
type: object
PromptVersion_Detail:
required:
- template
Expand All @@ -4166,6 +4180,10 @@ components:
\ be 8 characters long"
template:
type: string
metadata:
$ref: '#/components/schemas/JsonNode_Detail'
change_description:
type: string
variables:
uniqueItems: true
type: array
Expand Down Expand Up @@ -4269,6 +4287,10 @@ components:
\ be 8 characters long"
template:
type: string
metadata:
$ref: '#/components/schemas/JsonNode_Public'
change_description:
type: string
created_at:
type: string
format: date-time
Expand Down
22 changes: 22 additions & 0 deletions sdks/code_generation/fern/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4075,6 +4075,10 @@ components:
template:
pattern: (?s)^\s*(\S.*\S|\S)\s*$
type: string
metadata:
$ref: '#/components/schemas/JsonNode'
change_description:
type: string
created_at:
type: string
format: date-time
Expand Down Expand Up @@ -4115,6 +4119,10 @@ components:
\ be 8 characters long"
template:
type: string
metadata:
$ref: '#/components/schemas/JsonNode'
change_description:
type: string
variables:
uniqueItems: true
type: array
Expand Down Expand Up @@ -4146,6 +4154,12 @@ components:
template:
pattern: (?s)^\s*(\S.*\S|\S)\s*$
type: string
metadata:
$ref: '#/components/schemas/JsonNode_Write'
change_description:
type: string
JsonNode_Detail:
type: object
PromptVersion_Detail:
required:
- template
Expand All @@ -4166,6 +4180,10 @@ components:
\ be 8 characters long"
template:
type: string
metadata:
$ref: '#/components/schemas/JsonNode_Detail'
change_description:
type: string
variables:
uniqueItems: true
type: array
Expand Down Expand Up @@ -4269,6 +4287,10 @@ components:
\ be 8 characters long"
template:
type: string
metadata:
$ref: '#/components/schemas/JsonNode_Public'
change_description:
type: string
created_at:
type: string
format: date-time
Expand Down
2 changes: 2 additions & 0 deletions sdks/python/src/opik/rest_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
FunctionCall,
JsonNode,
JsonNodeCompare,
JsonNodeDetail,
JsonNodePublic,
JsonNodeWrite,
JsonObjectSchema,
Expand Down Expand Up @@ -285,6 +286,7 @@
"GetSpansByProjectRequestType",
"JsonNode",
"JsonNodeCompare",
"JsonNodeDetail",
"JsonNodePublic",
"JsonNodeWrite",
"JsonObjectSchema",
Expand Down
17 changes: 17 additions & 0 deletions sdks/python/src/opik/rest_api/prompts/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..core.pydantic_utilities import parse_obj_as
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.json_node_write import JsonNodeWrite
from ..errors.bad_request_error import BadRequestError
from ..errors.conflict_error import ConflictError
from ..errors.unprocessable_entity_error import UnprocessableEntityError
Expand Down Expand Up @@ -91,6 +92,8 @@ def create_prompt(
id: typing.Optional[str] = OMIT,
description: typing.Optional[str] = OMIT,
template: typing.Optional[str] = OMIT,
metadata: typing.Optional[JsonNodeWrite] = OMIT,
change_description: typing.Optional[str] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> None:
"""
Expand All @@ -106,6 +109,10 @@ def create_prompt(
template : typing.Optional[str]
metadata : typing.Optional[JsonNodeWrite]
change_description : typing.Optional[str]
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Expand All @@ -130,6 +137,8 @@ def create_prompt(
"name": name,
"description": description,
"template": template,
"metadata": metadata,
"change_description": change_description,
},
headers={
"content-type": "application/json",
Expand Down Expand Up @@ -794,6 +803,8 @@ async def create_prompt(
id: typing.Optional[str] = OMIT,
description: typing.Optional[str] = OMIT,
template: typing.Optional[str] = OMIT,
metadata: typing.Optional[JsonNodeWrite] = OMIT,
change_description: typing.Optional[str] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> None:
"""
Expand All @@ -809,6 +820,10 @@ async def create_prompt(
template : typing.Optional[str]
metadata : typing.Optional[JsonNodeWrite]
change_description : typing.Optional[str]
request_options : typing.Optional[RequestOptions]
Request-specific configuration.
Expand Down Expand Up @@ -841,6 +856,8 @@ async def main() -> None:
"name": name,
"description": description,
"template": template,
"metadata": metadata,
"change_description": change_description,
},
headers={
"content-type": "application/json",
Expand Down
2 changes: 2 additions & 0 deletions sdks/python/src/opik/rest_api/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
from .function_call import FunctionCall
from .json_node import JsonNode
from .json_node_compare import JsonNodeCompare
from .json_node_detail import JsonNodeDetail
from .json_node_public import JsonNodePublic
from .json_node_write import JsonNodeWrite
from .json_object_schema import JsonObjectSchema
Expand Down Expand Up @@ -251,6 +252,7 @@
"FunctionCall",
"JsonNode",
"JsonNodeCompare",
"JsonNodeDetail",
"JsonNodePublic",
"JsonNodeWrite",
"JsonObjectSchema",
Expand Down
5 changes: 5 additions & 0 deletions sdks/python/src/opik/rest_api/types/json_node_detail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file was auto-generated by Fern from our API Definition.

import typing

JsonNodeDetail = typing.Dict[str, typing.Optional[typing.Any]]
3 changes: 3 additions & 0 deletions sdks/python/src/opik/rest_api/types/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ..core.pydantic_utilities import UniversalBaseModel
import typing
from .json_node import JsonNode
import datetime as dt
from .prompt_version import PromptVersion
from ..core.pydantic_utilities import IS_PYDANTIC_V2
Expand All @@ -13,6 +14,8 @@ class Prompt(UniversalBaseModel):
name: str
description: typing.Optional[str] = None
template: typing.Optional[str] = None
metadata: typing.Optional[JsonNode] = None
change_description: typing.Optional[str] = None
created_at: typing.Optional[dt.datetime] = None
created_by: typing.Optional[str] = None
last_updated_at: typing.Optional[dt.datetime] = None
Expand Down
3 changes: 3 additions & 0 deletions sdks/python/src/opik/rest_api/types/prompt_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..core.pydantic_utilities import UniversalBaseModel
import typing
import pydantic
from .json_node import JsonNode
import datetime as dt
from ..core.pydantic_utilities import IS_PYDANTIC_V2

Expand All @@ -20,6 +21,8 @@ class PromptVersion(UniversalBaseModel):
"""

template: str
metadata: typing.Optional[JsonNode] = None
change_description: typing.Optional[str] = None
variables: typing.Optional[typing.List[str]] = None
created_at: typing.Optional[dt.datetime] = None
created_by: typing.Optional[str] = None
Expand Down
3 changes: 3 additions & 0 deletions sdks/python/src/opik/rest_api/types/prompt_version_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..core.pydantic_utilities import UniversalBaseModel
import typing
import pydantic
from .json_node_detail import JsonNodeDetail
import datetime as dt
from ..core.pydantic_utilities import IS_PYDANTIC_V2

Expand All @@ -20,6 +21,8 @@ class PromptVersionDetail(UniversalBaseModel):
"""

template: str
metadata: typing.Optional[JsonNodeDetail] = None
change_description: typing.Optional[str] = None
variables: typing.Optional[typing.List[str]] = None
created_at: typing.Optional[dt.datetime] = None
created_by: typing.Optional[str] = None
Expand Down
3 changes: 3 additions & 0 deletions sdks/python/src/opik/rest_api/types/prompt_version_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ..core.pydantic_utilities import UniversalBaseModel
import typing
import pydantic
from .json_node_public import JsonNodePublic
import datetime as dt
from ..core.pydantic_utilities import IS_PYDANTIC_V2

Expand All @@ -20,6 +21,8 @@ class PromptVersionPublic(UniversalBaseModel):
"""

template: str
metadata: typing.Optional[JsonNodePublic] = None
change_description: typing.Optional[str] = None
created_at: typing.Optional[dt.datetime] = None
created_by: typing.Optional[str] = None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
import * as OpikApi from "../../../../index";
/**
* @example
* {
Expand All @@ -12,4 +13,6 @@ export interface PromptWrite {
name: string;
description?: string;
template?: string;
metadata?: OpikApi.JsonNodeWrite;
changeDescription?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
export declare type JsonNodeDetail = Record<string, unknown>;
5 changes: 5 additions & 0 deletions sdks/typescript/src/opik/rest_api/api/types/JsonNodeDetail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
/**
* This file was auto-generated by Fern from our API Definition.
*/
Object.defineProperty(exports, "__esModule", { value: true });
2 changes: 2 additions & 0 deletions sdks/typescript/src/opik/rest_api/api/types/Prompt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface Prompt {
name: string;
description?: string;
template?: string;
metadata?: OpikApi.JsonNode;
changeDescription?: string;
createdAt?: Date;
createdBy?: string;
lastUpdatedAt?: Date;
Expand Down
Loading

0 comments on commit 5906283

Please sign in to comment.