Skip to content

Latest commit

 

History

History
144 lines (109 loc) · 19.1 KB

File metadata and controls

144 lines (109 loc) · 19.1 KB

Chat

Overview

Available Operations

create

Run an assistant turn. The default response is JSON. HTTP clients request server-sent events by setting stream to true in the JSON body. An Accept: text/event-stream header does not replace stream.

Example Usage

from glean.api_client import Glean, models
import os


with Glean(
    api_token=os.getenv("GLEAN_API_TOKEN", ""),
) as glean:

    res = glean.chat.create(input="Summarize our parental leave policy as JSON.", store=True, text={
        "format_": {
            "type": models.PlatformChatJSONSchemaFormatType.JSON_SCHEMA,
            "name": "policy_summary",
            "schema_": {
                "type": "object",
                "properties": {
                    "eligible_employees": {
                        "type": "string",
                    },
                    "duration_weeks": {
                        "type": "integer",
                    },
                },
                "required": [
                    "eligible_employees",
                    "duration_weeks",
                ],
            },
            "strict": True,
        },
    })

    # Handle response
    print(res)

Parameters

Parameter Type Required Description
input models.PlatformChatCreateInput ✔️ Either a plain string (single user turn) or a chronological array of USER/ASSISTANT messages. The final array message must be USER.
store Optional[bool] ➖ When true (default), persist the interaction and return a conversation_id. When false, run ephemerally with no persistence.
conversation_id Optional[str] ➖ Continue an existing stored conversation. Incompatible with message-array input and with store: false.
text Optional[models.PlatformChatCreateText] ➖ Optional configuration for the assistant's text response. When format.type is JSON_SCHEMA, the response is constrained to the supplied JSON schema and returned in output[*].content[*].structured_output. Structured output is not supported when stream is true.
retries Optional[utils.RetryConfig] ➖ Configuration to override the default retry behavior of the client.

Response

models.PlatformChatCompletedResponse

Errors

Error Type Status Code Content Type
errors.PlatformProblemDetailError 400, 401, 403, 404, 408, 413, 422, 429 application/problem+json
errors.PlatformProblemDetailError 500, 503 application/problem+json
errors.GleanError 4XX, 5XX */*

create_stream

SDK-only logical operation. HTTP clients must call the base path; the URL fragment is not sent. Run an assistant turn as server-sent events. HTTP clients request this mode by setting stream to true in the JSON body.

Example Usage

from glean.api_client import Glean, models
import os


with Glean(
    api_token=os.getenv("GLEAN_API_TOKEN", ""),
) as glean:

    res = glean.chat.create_stream(input="Summarize our parental leave policy as JSON.", store=True, text={
        "format_": {
            "type": models.PlatformChatJSONSchemaFormatType.JSON_SCHEMA,
            "name": "policy_summary",
            "schema_": {
                "type": "object",
                "properties": {
                    "eligible_employees": {
                        "type": "string",
                    },
                    "duration_weeks": {
                        "type": "integer",
                    },
                },
                "required": [
                    "eligible_employees",
                    "duration_weeks",
                ],
            },
            "strict": True,
        },
    })

    with res as event_stream:
        for event in event_stream:
            # handle event
            print(event, flush=True)

Parameters

Parameter Type Required Description
input models.PlatformChatCreateStreamInput ✔️ Either a plain string (single user turn) or a chronological array of USER/ASSISTANT messages. The final array message must be USER.
store Optional[bool] ➖ When true (default), persist the interaction and return a conversation_id. When false, run ephemerally with no persistence.
conversation_id Optional[str] ➖ Continue an existing stored conversation. Incompatible with message-array input and with store: false.
text Optional[models.PlatformChatCreateStreamText] ➖ Optional configuration for the assistant's text response. When format.type is JSON_SCHEMA, the response is constrained to the supplied JSON schema and returned in output[*].content[*].structured_output. Structured output is not supported when stream is true.
retries Optional[utils.RetryConfig] ➖ Configuration to override the default retry behavior of the client.

Response

Union[eventstreaming.EventStream[models.PlatformChatStreamEventServerSentEvent], eventstreaming.EventStreamAsync[models.PlatformChatStreamEventServerSentEvent]]

Errors

Error Type Status Code Content Type
errors.PlatformProblemDetailError 400, 401, 403, 404, 408, 413, 422, 429 application/problem+json
errors.PlatformProblemDetailError 500, 503 application/problem+json
errors.GleanError 4XX, 5XX */*