Skip to content

Latest commit

 

History

History
172 lines (132 loc) · 9.35 KB

File metadata and controls

172 lines (132 loc) · 9.35 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

package hello.world;

import com.glean.api_client.glean_api_client.Glean;
import com.glean.api_client.glean_api_client.models.components.PlatformChatJsonSchemaFormat;
import com.glean.api_client.glean_api_client.models.components.PlatformChatJsonSchemaFormatType;
import com.glean.api_client.glean_api_client.models.errors.PlatformProblemDetailException;
import com.glean.api_client.glean_api_client.models.operations.*;
import java.lang.Exception;
import java.util.List;
import java.util.Map;

public class Application {

    public static void main(String[] args) throws PlatformProblemDetailException, Exception {

        Glean sdk = Glean.builder()
                .apiToken(System.getenv().getOrDefault("GLEAN_API_TOKEN", ""))
            .build();

        PlatformChatCreateRequest req = PlatformChatCreateRequest.builder()
                .input(PlatformChatCreateInput.of("Summarize our parental leave policy as JSON."))
                .text(PlatformChatCreateText.builder()
                    .format(PlatformChatCreateFormat.of(PlatformChatJsonSchemaFormat.builder()
                        .type(PlatformChatJsonSchemaFormatType.JSON_SCHEMA)
                        .name("policy_summary")
                        .schema(Map.ofEntries(
                            Map.entry("type", "object"),
                            Map.entry("properties", Map.ofEntries(
                                Map.entry("eligible_employees", Map.ofEntries(
                                    Map.entry("type", "string"))),
                                Map.entry("duration_weeks", Map.ofEntries(
                                    Map.entry("type", "integer"))))),
                            Map.entry("required", List.of(
                                "eligible_employees",
                                "duration_weeks"))))
                        .build()))
                    .build())
                .build();

        PlatformChatCreateResponse res = sdk.chat().create()
                .request(req)
                .call();

        if (res.platformChatCompletedResponse().isPresent()) {
            System.out.println(res.platformChatCompletedResponse().get());
        }
    }
}

Parameters

Parameter Type Required Description
request PlatformChatCreateRequest ✔️ The request object to use for the request.

Response

PlatformChatCreateResponse

Errors

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

createStream

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

package hello.world;

import com.glean.api_client.glean_api_client.Glean;
import com.glean.api_client.glean_api_client.models.components.*;
import com.glean.api_client.glean_api_client.models.errors.PlatformProblemDetailException;
import com.glean.api_client.glean_api_client.models.operations.*;
import com.glean.api_client.glean_api_client.utils.EventStream;
import java.lang.Exception;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class Application {

    public static void main(String[] args) throws PlatformProblemDetailException, Exception {

        Glean sdk = Glean.builder()
                .apiToken(System.getenv().getOrDefault("GLEAN_API_TOKEN", ""))
            .build();

        PlatformChatCreateStreamRequest req = PlatformChatCreateStreamRequest.builder()
                .input(PlatformChatCreateStreamInput.of("Summarize our parental leave policy as JSON."))
                .text(PlatformChatCreateStreamText.builder()
                    .format(PlatformChatCreateStreamFormat.of(PlatformChatJsonSchemaFormat.builder()
                        .type(PlatformChatJsonSchemaFormatType.JSON_SCHEMA)
                        .name("policy_summary")
                        .schema(Map.ofEntries(
                            Map.entry("type", "object"),
                            Map.entry("properties", Map.ofEntries(
                                Map.entry("eligible_employees", Map.ofEntries(
                                    Map.entry("type", "string"))),
                                Map.entry("duration_weeks", Map.ofEntries(
                                    Map.entry("type", "integer"))))),
                            Map.entry("required", List.of(
                                "eligible_employees",
                                "duration_weeks"))))
                        .build()))
                    .build())
                .build();

        PlatformChatCreateStreamResponse res = sdk.chat().createStream()
                .request(req)
                .call();

        // handle event stream, must be closed after use!
        try (EventStream<PlatformChatStreamEventServerSentEvent> events = res.events()) {
            // Option 1: Use for-each loop
            for (PlatformChatStreamEventServerSentEvent event : events) {
                System.out.println(event);
            }

            // Option 2: Use Stream API
            try (Stream<PlatformChatStreamEventServerSentEvent> stream = events.stream()) {
                 stream.forEach(System.out::println);
            }
        }
    }
}

Parameters

Parameter Type Required Description
request PlatformChatCreateStreamRequest ✔️ The request object to use for the request.

Response

PlatformChatCreateStreamResponse

Errors

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