### Setup and Run Examples Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Instructions for cloning the repository, installing dependencies, setting up environment variables, and running the examples. ```bash # Clone the repository git clone https://github.com/graphlit/graphlit-client-typescript.git cd graphlit-client-typescript # Install dependencies npm install # Set up your environment variables cp .env.example .env # Edit .env with your Graphlit credentials # Run the examples npm test test/readme-simple.test.ts ``` -------------------------------- ### Install Optional Streaming Provider SDKs Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/configuration.md Install the necessary SDKs for native streaming support with various AI providers. Install only the packages you intend to use. ```bash npm install openai @anthropic-ai/sdk groq-sdk cohere-ai @mistralai/mistralai @google/genai ``` -------------------------------- ### Install Groq SDK for Streaming Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the Groq SDK, which is OpenAI-compatible, to enable streaming responses. ```bash npm install groq-sdk ``` -------------------------------- ### Install Anthropic SDK for Streaming Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the Anthropic SDK to enable streaming responses from Anthropic models. ```bash npm install @anthropic-ai/sdk ``` -------------------------------- ### Install Cohere SDK for Streaming Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the Cohere AI SDK to enable streaming responses from Cohere models. ```bash npm install cohere-ai ``` -------------------------------- ### Install Mistral SDK for Streaming Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the Mistral AI SDK to enable streaming responses from Mistral models. ```bash npm install @mistralai/mistralai ``` -------------------------------- ### Run Streaming Example Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/test/README.md Execute the streaming example script to demonstrate real-time token streaming, event handling, tool calling, and multi-LLM provider support. Requires API keys. ```bash node streaming-example.js ``` -------------------------------- ### Install OpenAI SDK for Streaming Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the OpenAI SDK to enable streaming responses from OpenAI models. ```bash npm install openai ``` -------------------------------- ### Install Graphlit SDK Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the Graphlit client library for your project using npm. ```bash npm install graphlit-client ``` -------------------------------- ### Install Google SDK for Streaming Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the Google Generative AI SDK to enable streaming responses from Google models. ```bash npm install @google/genai ``` -------------------------------- ### Install Dependencies Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/test/README.md Install project dependencies using npm. ```bash npm install ``` -------------------------------- ### Streaming Example Expected Output Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/test/README.md Example output from the streaming example script, demonstrating real-time token streaming and conversation completion. ```text ๐ŸŒŠ Graphlit Streaming Examples ๐Ÿ”„ Basic Streaming Example โœ… OpenAI client configured ๐Ÿš€ Starting streaming conversation... ๐ŸŽฌ Conversation started Conversation ID: conv_123 Once upon a time, in a small art studio... [real-time streaming tokens] ๐ŸŽ‰ Conversation completed! Final conversation ID: conv_123 ๐Ÿ“Š Final message buffer length: 245 characters ``` -------------------------------- ### Install AWS Bedrock Runtime SDK for Streaming Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the AWS Bedrock Runtime SDK to enable streaming responses from Claude models hosted on AWS Bedrock. ```bash npm install @aws-sdk/client-bedrock-runtime ``` -------------------------------- ### Create ToolDefinitionInput Example Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/types.md Shows an example of creating a `ToolDefinitionInput` for a 'get_weather' tool. This demonstrates how to define the tool's name, description, and its parameter schema. ```typescript const weatherTool: ToolDefinitionInput = { name: "get_weather", description: "Get current weather for a city", schema: JSON.stringify({ type: "object", properties: { city: { type: "string", description: "City name" }, }, required: ["city"], }), }; ``` -------------------------------- ### Install Graphlit SDK and Set Credentials Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Install the Graphlit SDK using npm and set the necessary environment variables for authentication. Ensure you have a Graphlit account to obtain your organization, environment, and secret values. ```bash npm install graphlit-client export GRAPHLIT_ORGANIZATION_ID=your_org_id export GRAPHLIT_ENVIRONMENT_ID=your_env_id export GRAPHLIT_JWT_SECRET=your_secret ``` -------------------------------- ### Create EntityReferenceInput Example Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/types.md Provides an example of creating an `EntityReferenceInput` object using an entity's ID. This is useful when you need to refer to a specific entity in an API call. ```typescript const specRef: EntityReferenceInput = { id: specification.createSpecification.id }; ``` -------------------------------- ### Tool Handler Example Implementations Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/types.md Illustrates how to implement `ToolHandler` functions for different tools, including fetching weather data and ingesting documents with artifact collection. ```typescript const toolHandlers: Record = { get_weather: async (args: { city: string }) => { const response = await fetch(`https://api.weather.com/${args.city}`); return await response.json(); }, ingest_document: async (args: { url: string }, artifacts) => { const promise = client.ingestUri(args.url, args.url, undefined, true); artifacts?.addPending(promise.then(r => ({ id: r.ingestUri.id }))); return { status: "ingesting" }; }, }; ``` -------------------------------- ### Example: Query and Process Facts Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/api-reference/specifications-and-knowledge.md Demonstrates how to query facts using the client and iterate over the results to log subject, predicate, and object details. Ensure the client is initialized before use. ```typescript const facts = await client.queryFacts({ contains: "founded", first: 100, }); facts.facts.edges.forEach(edge => { const fact = edge.node; console.log(`Subject: ${fact.subject?.name}`); console.log(`Predicate: ${fact.predicate?.name}`); console.log(`Object: ${fact.object?.name}`); }); ``` -------------------------------- ### Graphlit Client Constructor Examples Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/api-reference/graphlit-client.md Demonstrates different ways to initialize the Graphlit client, including using environment variables, explicit parameters, an options object, or a pre-signed token. The options object is recommended for its flexibility. ```typescript import { Graphlit, Types } from "graphlit-client"; // Method 1: Using environment variables const client = new Graphlit(); // Method 2: Explicit parameters const client = new Graphlit( "your-organization-id", "your-environment-id", "your-jwt-secret" ); // Method 3: Options object (recommended) const client = new Graphlit({ organizationId: "your-organization-id", environmentId: "your-environment-id", jwtSecret: "your-jwt-secret", retryConfig: { maxAttempts: 10, initialDelay: 500, maxDelay: 60000, }, }); // Method 4: With pre-signed token (jwtSecret not required) const client = new Graphlit({ organizationId: "your-organization-id", environmentId: "your-environment-id", token: "your-pre-signed-token", }); ``` -------------------------------- ### Get Product by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific product using its unique identifier. ```typescript await graphlit.getProduct(id: string): Promise ``` -------------------------------- ### Import and Initialize Graphlit Client Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Import the Graphlit class and necessary types, then create a new instance of the Graphlit client. This is the basic setup required to interact with the Graphlit API. ```typescript import { Graphlit } from 'graphlit-client'; import * as Types from 'graphlit-client/dist/generated/graphql-types'; const graphlit = new Graphlit(); ``` -------------------------------- ### Quick Example of Streaming Agent with Reasoning Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md This snippet demonstrates how to stream an agent's response and log reasoning updates and message updates as they occur. Use this for a basic integration of reasoning support. ```typescript await client.streamAgent( "What's 15% of 240? Think step by step.", (event) => { if (event.type === "reasoning_update") { console.log("๐Ÿค” Model thinking:", event.content); } else if (event.type === "message_update") { console.log("๐Ÿ’ฌ Answer:", event.message.message); } }, undefined, { id: specificationId }, ); ``` -------------------------------- ### Query Project Credits Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves project credits. Requires a start date and duration. ```typescript await graphlit.queryProjectCredits(startDate: Types.Scalars["DateTime"]["input"], duration: Types.Scalars["TimeSpan"]["input"]): Promise ``` -------------------------------- ### Basic Tests Expected Output Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/test/README.md Example output from the basic test runner, showing passed tests for client creation, method existence, and LLM provider integration. ```text ๐Ÿงช Graphlit Streaming Test Runner ๐Ÿ“ฆ Basic Client Tests ๐Ÿ” Testing: Client creation with valid credentials โœ… PASSED ๐ŸŒŠ Streaming Tests ๐Ÿ” Testing: streamConversation method exists โœ… PASSED ๐Ÿค– LLM Provider Tests ๐Ÿ” Testing: OpenAI integration (if installed) ๐Ÿ“ฆ OpenAI SDK detected โœ… OpenAI client creation works โœ… PASSED ๐Ÿ“Š Test Summary โœ… Passed: 8 โŒ Failed: 0 ๐Ÿ“ˆ Total: 8 ๐ŸŽ‰ All tests passed! ``` -------------------------------- ### Using Reasoning Detection with a Specification Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md This example shows how to create a reasoning-capable specification and then stream an agent's response, capturing and logging both reasoning steps and the final message. It's useful for detailed analysis of AI thought processes. ```typescript // Create a specification with a reasoning-capable model const spec = await client.createSpecification({ name: "Reasoning Assistant", serviceType: Types.ModelServiceTypes.Bedrock, bedrock: { model: Types.BedrockModels.NovaPremier, temperature: 0.7, }, }); // Track reasoning steps const reasoningSteps: string[] = []; await client.streamAgent( "Analyze the pros and cons of remote work. Think carefully.", (event) => { switch (event.type) { case "reasoning_update": // Capture model's thinking process reasoningSteps.push(event.content); console.log(`๐Ÿง  Thinking (${event.format}):`, event.content); if (event.isComplete) { console.log("โœ… Reasoning complete!"); } break; case "message_update": // The actual answer (reasoning removed) console.log("Answer:", event.message.message); break; } }, undefined, { id: spec.createSpecification!.id }, ); ``` -------------------------------- ### Handle Authentication Errors with Invalid Credentials Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/errors.md Demonstrates initializing the client with invalid credentials, which will result in an authentication error. Verify credentials and ensure they follow the correct format (GUIDs for IDs). ```typescript const client = new Graphlit({ organizationId: "invalid-id", // โ† Invalid GUID format environmentId: "env-id", jwtSecret: "secret", }); // Error: "Invalid organization ID format. Expected a valid GUID..." ``` -------------------------------- ### Handle Tool Execution Failure Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/errors.md This example demonstrates how to set up tool handlers and process tool update events to catch and log tool execution failures. Ensure tool handlers have proper error handling and are robust. ```typescript const toolHandlers = { fetch_data: async (args) => { throw new Error("Database connection failed"); }, }; await client.streamAgent( "Use the fetch_data tool", (event) => { if (event.type === "tool_update" && event.status === "failed") { console.error("Tool failed:", event.error); } }, undefined, spec, [{ name: "fetch_data", ... }], toolHandlers ); ``` -------------------------------- ### Update Dependencies for Graphlit SDK Migration Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Remove the old @google/generative-ai SDK and install the new @google/genai SDK using npm. ```bash # Remove old SDK npm uninstall @google/generative-ai # Install new SDK npm install @google/genai ``` -------------------------------- ### Initialize Graphlit Client and Stream AI Message Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Initialize the Graphlit client using environment variables and stream an AI message. This example demonstrates creating a completion specification and handling the streamed response. ```typescript import { Graphlit, Types } from "graphlit-client"; const client = new Graphlit(); // Uses env vars: GRAPHLIT_ORGANIZATION_ID, GRAPHLIT_ENVIRONMENT_ID, GRAPHLIT_JWT_SECRET // First, create a specification (or use your project default) const spec = await client.createSpecification({ name: "Assistant", type: Types.SpecificationTypes.Completion, serviceType: Types.ModelServiceTypes.OpenAi, openAI: { model: Types.OpenAiModels.Gpt4O_128K, }, }); // Start chatting with AI await client.streamAgent( "Tell me a joke", (event) => { if (event.type === "message_update") { console.log(event.message.message); } }, undefined, // conversationId (optional) { id: spec.createSpecification.id }, // specification ); ``` -------------------------------- ### Query Project Tokens Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves project tokens. Requires a start date and duration to be specified. ```typescript await graphlit.queryProjectTokens(startDate: Types.Scalars["DateTime"]["input"], duration: Types.Scalars["TimeSpan"]["input"]): Promise ``` -------------------------------- ### Provide User Feedback for Errors Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/errors.md This example demonstrates how to provide user-friendly feedback based on error recoverability. It suggests showing different messages or UI states for temporary versus permanent errors. ```typescript await client.streamAgent(prompt, (event) => { if (event.type === "error") { if (event.error.recoverable) { console.error("Temporary issue โ€” retrying..."); // Show spinner or "retrying" message to user } else { console.error("Unable to process your request. Please try again later."); // Show permanent error message to user } } }, conversationId, spec); ``` -------------------------------- ### Lookup Project Credits Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Use this to lookup credit usage for a project, optionally specifying a start date and duration. ```typescript await graphlit.lookupProjectCredits( correlationId: string, startDate?: Types.Scalars["DateTime"]["input"], duration?: Types.Scalars["TimeSpan"]["input"], ): Promise ``` -------------------------------- ### Stream Agent Conversation with Event Handling Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/api-reference/conversation-and-streaming.md Initiates a streaming conversation with an agent, processing various event types like conversation start, context updates, message and tool updates, reasoning, completion, and errors. Includes setup for UI stop buttons, tool definitions, tool handlers, and content filters. ```typescript const controller = new AbortController(); // Stop button in UI document.getElementById("stop").onclick = () => controller.abort(); try { await client.streamAgent( "Analyze these documents and provide insights", // Event handler (event) => { switch (event.type) { case "conversation_started": console.log("Conversation:", event.conversationId); break; case "context_window": const usage = event.usage; console.log( `Context: ${usage.usedTokens}/${usage.maxTokens} tokens (${usage.percentage}%)` ); break; case "message_update": if (event.isStreaming) { process.stdout.write(event.message.message); } console.log(`Metrics:`, event.metrics); break; case "tool_update": console.log(`Tool '${event.toolCall.name}': ${event.status}`); if (event.status === "completed") { console.log("Result:", event.result); } if (event.status === "failed") { console.error("Error:", event.error); } break; case "reasoning_update": console.log("๐Ÿง  Model thinking:", event.content); if (event.isComplete) { console.log("โœ… Reasoning complete"); } break; case "conversation_completed": console.log("\n๐Ÿ“ Final response:", event.message.message); console.log("๐Ÿ“Š Usage:", event.usage); console.log("โฑ๏ธ Metrics:", event.metrics); break; case "error": if (error.recoverable) { console.warn("โš ๏ธ Transient error, retried by SDK:", event.error.message); } else { console.error("โŒ Permanent error:", event.error.message); } break; } }, undefined, // conversationId - will create new // Specification { id: specificationId }, // Tools (optional) [ { name: "fetch_document", description: "Fetch a document from storage", schema: JSON.stringify({ type: "object", properties: { documentId: { type: "string" } }, required: ["documentId"], }), }, ], // Tool handlers (optional) { fetch_document: async (args, artifacts) => { const doc = await client.getContent(args.documentId); return { title: doc.content.name, content: doc.content.text, }; }, }, // Options { maxToolRounds: 5, abortSignal: controller.signal, contextStrategy: { toolResultTokenLimit: 8192, toolRoundLimit: 10, rebudgetThreshold: 0.75, }, additionalSystemInstructions: "Always cite your sources.", skills: [{ id: "skill-123" }], }, // Optional: binary data undefined, // mimeType undefined, // data // Content filters { // Can retrieve from these contents contents: [{ id: "doc-1" }, { id: "doc-2" }], }, { // Always include these contents (no retrieval) contents: [{ id: "context-doc" }], } ); } catch (error) { if (controller.signal.aborted) { console.log("โน๏ธ Stopped by user"); } else if (error instanceof ProviderError) { console.error(`Provider error: ${error.provider} (${error.statusCode})`); } else { console.error("Error:", error); } } ``` -------------------------------- ### Create Workflow for Content Summarization Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md This snippet demonstrates how to create a workflow that automatically summarizes ingested content using an AI model specification. It configures a summarization specification and then creates a workflow that utilizes it, setting it as the default for the project. Content ingested after this setup will be automatically summarized. ```typescript import { Graphlit, Types } from "graphlit-client"; const client = new Graphlit(); // Uses env vars: GRAPHLIT_ORGANIZATION_ID, GRAPHLIT_ENVIRONMENT_ID, GRAPHLIT_JWT_SECRET // Create specifications for AI models const summarizationSpec = await client.createSpecification({ name: "Summarizer", type: Types.SpecificationTypes.Summarization, serviceType: Types.ModelServiceTypes.OpenAi, openAI: { model: Types.OpenAiModels.Gpt4O_128K, }, }); // Create a workflow that summarizes all content const workflow = await client.createWorkflow({ name: "Document Intelligence", preparation: { summarizations: [ { type: Types.SummarizationTypes.Summary, specification: { id: summarizationSpec.createSpecification.id }, }, ], }, }); // Set workflow as default for project await client.updateProject({ workflow: { id: workflow.createWorkflow.id }, }); // Now all content will be automatically summarized const content = await client.ingestUri( "https://example.com/report.pdf", // uri ); ``` -------------------------------- ### Suggest Follow-up Prompts in TypeScript Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Suggests potential follow-up prompts for a given conversation. Useful for guiding user interaction or generating next steps. ```typescript await graphlit.suggestConversation( id: string, count?: number, correlationId?: string, ): Promise ``` -------------------------------- ### Streaming Agent Call with Custom Options Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/configuration.md Example of calling `streamAgent` with specific configurations for tool rounds, smoothing, context strategy, system instructions, and fallbacks. This demonstrates how to override default behaviors for fine-grained control. ```typescript await client.streamAgent( "Explain quantum computing", (event) => { if (event.type === "message_update") { process.stdout.write(event.message.message); } }, undefined, { id: specificationId }, undefined, undefined, { maxToolRounds: 50, smoothingEnabled: true, smoothingDelay: 50, contextStrategy: { toolResultTokenLimit: 16384, toolRoundLimit: 20, rebudgetThreshold: 0.8, }, additionalSystemInstructions: "Always cite your sources.", fallbacks: [ { id: fallbackSpecId1 }, { id: fallbackSpecId2 }, ], } ); ``` -------------------------------- ### Quick Example: Cancel AI Generation with AbortController Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md This snippet demonstrates how to cancel a streaming AI generation using the standard Web API AbortController. It shows how to attach an event listener to a stop button to trigger the cancellation. ```typescript const controller = new AbortController(); // Add a stop button document.getElementById("stop").onclick = () => controller.abort(); try { await client.streamAgent( "Write a 10,000 word essay about quantum computing...", (event) => { if (event.type === "message_update") { console.log(event.message.message); } }, undefined, { id: specificationId }, undefined, // tools undefined, // toolHandlers { abortSignal: controller.signal }, // Pass the signal ); } catch (error) { if (controller.signal.aborted) { console.log("โœ‹ Generation stopped by user"); } } ``` -------------------------------- ### Get Alert Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Looks up a specific alert by its ID. ```typescript await graphlit.getAlert(id: string): Promise ``` -------------------------------- ### Build a Knowledge Base Assistant Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Create an AI assistant that answers questions from your documents. This involves initializing the client, creating a specification, uploading documents, and then streaming agent responses within a conversation context. ```typescript import { Graphlit, Types } from "graphlit-client"; class KnowledgeAssistant { private client: Graphlit; private conversationId?: string; private specificationId?: string; private contentIds: string[] = []; constructor() { this.client = new Graphlit(); // Uses env vars: GRAPHLIT_ORGANIZATION_ID, GRAPHLIT_ENVIRONMENT_ID, GRAPHLIT_JWT_SECRET } async initialize() { // Create a specification for the assistant const spec = await this.client.createSpecification({ name: "Knowledge Assistant", type: Types.SpecificationTypes.Completion, serviceType: Types.ModelServiceTypes.OpenAi, openAI: { model: Types.OpenAiModels.Gpt4O_128K, temperature: 0.7, }, }); this.specificationId = spec.createSpecification?.id; } async uploadDocuments(urls: string[]) { console.log("๐Ÿ“š Uploading documents..."); for (const url of urls) { const content = await this.client.ingestUri( url, // uri url.split("/").pop() || "Document", // name undefined, // id true, // isSynchronous - wait for processing ); this.contentIds.push(content.ingestUri.id); } console.log("โœ… Documents uploaded!"); // Wait for content to be indexed await new Promise((resolve) => setTimeout(resolve, 5000)); } async ask(question: string) { // Create conversation with content filter if not exists if (!this.conversationId && this.contentIds.length > 0) { const conversation = await this.client.createConversation({ filter: { contents: this.contentIds.map((id) => ({ id })) }, }); this.conversationId = conversation.createConversation?.id; } await this.client.streamAgent( question, (event) => { if (event.type === "conversation_started" && !this.conversationId) { this.conversationId = event.conversationId; } else if (event.type === "message_update") { process.stdout.write(event.message.message); } }, this.conversationId, // Maintains conversation context { id: this.specificationId! }, // specification ); } } // Usage const assistant = new KnowledgeAssistant(); await assistant.initialize(); // Upload your documents await assistant.uploadDocuments([ "https://arxiv.org/pdf/2103.15348.pdf", "https://arxiv.org/pdf/1706.03762.pdf", ]); // Ask questions await assistant.ask("What are these papers about?"); await assistant.ask("How do they relate to each other?"); ``` -------------------------------- ### Compare AI Responses from Multiple Providers Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Demonstrates how to set up and use multiple LLM providers (OpenAI, Cohere, Groq) and compare their responses to the same prompt. Requires API keys for each provider to be set in environment variables. ```typescript import { Graphlit, Types } from "graphlit-client"; const client = new Graphlit(); // Set up multiple providers if (process.env.OPENAI_API_KEY) { const { OpenAI } = await import("openai"); client.setOpenAIClient(new OpenAI()); } if (process.env.COHERE_API_KEY) { const { CohereClientV2 } = await import("cohere-ai"); client.setCohereClient( new CohereClientV2({ token: process.env.COHERE_API_KEY }), ); } if (process.env.GROQ_API_KEY) { const { Groq } = await import("groq-sdk"); client.setGroqClient(new Groq({ apiKey: process.env.GROQ_API_KEY })); } // Create specifications for different providers const providers = [ { name: "OpenAI GPT-4o", serviceType: Types.ModelServiceTypes.OpenAi, openAI: { model: Types.OpenAiModels.Gpt4O_128K }, }, { name: "Cohere Command R+", serviceType: Types.ModelServiceTypes.Cohere, cohere: { model: Types.CohereModels.CommandRPlus }, }, { name: "Groq Llama", serviceType: Types.ModelServiceTypes.Groq, groq: { model: Types.GroqModels.Llama_3_3_70B }, }, ]; // Compare responses for (const provider of providers) { console.log(`\n๐Ÿค– ${provider.name}:`); const spec = await client.createSpecification({ ...provider, type: Types.SpecificationTypes.Completion, }); await client.streamAgent( "Explain quantum computing in simple terms", (event) => { if (event.type === "message_update") { process.stdout.write(event.message.message); } }, undefined, { id: spec.createSpecification.id }, ); } ``` -------------------------------- ### Get Current User Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves the details of the currently authenticated user. ```typescript await graphlit.getUser(): Promise ``` -------------------------------- ### Get User by Identifier Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Looks up a user using their external identifier. ```typescript await graphlit.getUserByIdentifier(identifier: string): Promise ``` -------------------------------- ### Get a Medical Procedure Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Lookup a specific medical procedure by its unique identifier. ```typescript await graphlit.getMedicalProcedure(id: string): Promise ``` -------------------------------- ### Get a Medical Device Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Lookup a specific medical device by its unique identifier. ```typescript await graphlit.getMedicalDevice(id: string): Promise ``` -------------------------------- ### Create Product Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Creates a new product entity with the provided input data. ```typescript await graphlit.createProduct(product: Types.ProductInput): Promise ``` -------------------------------- ### Get Event by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific event using its unique identifier. ```typescript await graphlit.getEvent(id: string): Promise ``` -------------------------------- ### Run Agent with Options Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/api-reference/conversation-and-streaming.md Demonstrates how to use the runAgent method with custom options for maxTurns, timeout, and stuckDetectionThreshold. It also shows how to process different event types like tool updates and message updates. ```typescript const result = await client.runAgent( "Research and summarize the latest developments in AI safety", (event) => { if (event.type === "tool_update") { console.log(`[Tool] ${event.toolCall.name}: ${event.status}`); } else if (event.type === "message_update") { process.stdout.write(event.message.message); } }, undefined, // conversationId { id: specificationId }, tools, toolHandlers, { maxTurns: 20, timeout: 5 * 60 * 1000, // 5 minutes stuckDetectionThreshold: 3, } ); console.log("\n๐Ÿ“Š Results:"); console.log("Status:", result.status); // "completed", "stuck", or "timeout" console.log("Turns:", result.turns); console.log("Message:", result.message); if (result.status === "stuck") { console.warn("Agent got stuck in a loop"); } else if (result.status === "timeout") { console.warn("Agent exceeded time limit"); } ``` -------------------------------- ### Get Organization by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a single organization entity by its unique identifier. ```typescript await graphlit.getOrganization(id: string): Promise ``` -------------------------------- ### Get Person by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a single person entity by their unique identifier. ```typescript await graphlit.getPerson(id: string): Promise ``` -------------------------------- ### Initialize Graphlit Client with Environment Variables Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/README.md Initializes the Graphlit client using environment variables. This is the recommended approach for configuration. ```typescript const client = new Graphlit(); ``` -------------------------------- ### Get Workflow by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific workflow using its unique identifier. ```typescript await graphlit.getWorkflow(id: string): Promise ``` -------------------------------- ### Get Feed by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific feed using its unique identifier. ```typescript await graphlit.getFeed(id: string): Promise ``` -------------------------------- ### Run Basic Tests Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/test/README.md Execute the basic test runner to validate client instantiation, method availability, and type interfaces. No API keys are required. ```bash npm test ``` -------------------------------- ### Get Collection by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a single collection using its unique identifier. ```typescript await graphlit.getCollection(id: string): Promise ``` -------------------------------- ### Get Repo by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific repository (repo) using its unique identifier. ```typescript await graphlit.getRepo(id: string): Promise ``` -------------------------------- ### Get Label Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Use this function to retrieve a specific label by its ID. Returns a GetLabelQuery. ```typescript await graphlit.getLabel(id: string): Promise ``` -------------------------------- ### Instantiate Graphlit Client with Environment Variables Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/configuration.md Recommended for production environments. The client automatically reads configuration from environment variables like GRAPHLIT_ORGANIZATION_ID and GRAPHLIT_ENVIRONMENT_ID. ```typescript import { Graphlit, Types } from "graphlit-client"; // Method 1: Environment variables (recommended for production) const client = new Graphlit(); ``` -------------------------------- ### Get Category Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Use this function to retrieve a specific category by its ID. Returns a GetCategoryQuery. ```typescript await graphlit.getCategory(id: string): Promise ``` -------------------------------- ### Graphlit Client Constructor Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/INDEX.md Initializes the Graphlit Client with various configuration options. ```APIDOC ## Constructor ### Description Initializes the Graphlit Client with all available options, including API keys, endpoint configurations, and retry settings. ### Method Constructor ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { GraphlitClient } from "@graphlit/client-typescript"; const client = new GraphlitClient({ apiKey: "YOUR_API_KEY", apiBaseUrl: "https://api.graphlit.com/v1", // ... other options }); ``` ### Response #### Success Response (200) N/A (Constructor) #### Response Example N/A (Constructor) ``` -------------------------------- ### Configure Multiple Streaming Providers Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md Set up clients for multiple LLM providers by checking for API keys in environment variables and initializing the respective SDK clients. This allows the Graphlit client to use different providers based on availability. ```typescript import { Graphlit } from "graphlit-client"; const client = new Graphlit(); // Example: Set up multiple streaming providers if (process.env.OPENAI_API_KEY) { const { OpenAI } = await import("openai"); client.setOpenAIClient(new OpenAI()); } if (process.env.COHERE_API_KEY) { const { CohereClientV2 } = await import("cohere-ai"); client.setCohereClient( new CohereClientV2({ token: process.env.COHERE_API_KEY }), ); } if (process.env.GROQ_API_KEY) { const { Groq } = await import("groq-sdk"); client.setGroqClient(new Groq({ apiKey: process.env.GROQ_API_KEY })); } // Then create specifications for any provider const spec = await client.createSpecification({ name: "Multi-Provider Assistant", type: Types.SpecificationTypes.Completion, serviceType: Types.ModelServiceTypes.Cohere, // or any supported provider cohere: { model: Types.CohereModels.CommandRPlus, temperature: 0.7, }, }); ``` -------------------------------- ### Get a View by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Use this to retrieve a specific view by its ID. Returns a `GetViewQuery`. ```typescript await graphlit.getView(id: string): Promise ``` -------------------------------- ### Create Repo Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Creates a new repository (repo) entity with the provided input data. ```typescript await graphlit.createRepo(repo: Types.RepoInput): Promise ``` -------------------------------- ### Get a Connector by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Use this to retrieve a specific connector by its ID. Returns a `GetConnectorQuery`. ```typescript await graphlit.getConnector(id: string): Promise ``` -------------------------------- ### Initialize Graphlit Client with Options Object Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/README.md Initializes the Graphlit client by providing an options object with specific configuration values. ```typescript const client = new Graphlit({ organizationId: "...", environmentId: "...", jwtSecret: "...", retryConfig: { maxAttempts: 10 }, }); ``` -------------------------------- ### Get Medical Drug by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific medical drug entity by its unique ID. ```typescript await graphlit.getMedicalDrug(id: string): Promise ``` -------------------------------- ### Get Medical Guideline by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific medical guideline entity by its unique ID. ```typescript await graphlit.getMedicalGuideline(id: string): Promise ``` -------------------------------- ### createSoftware Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Creates a new software entity with the provided input data. This method returns a promise that resolves to the mutation result. ```APIDOC ## createSoftware ### Description Creates a software entity. ### Method `createSoftware` ### Parameters - **software** (Types.SoftwareInput) - Required - The input data for the software entity. ### Response - **createSoftware** (Types.CreateSoftwareMutation) - The result of the software creation mutation. ``` -------------------------------- ### Initialize Graphlit Client Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/README.md Initialize the Graphlit client with your organization, environment, and JWT secret credentials. Ensure these are set as environment variables. ```typescript import { Graphlit, Types } from "graphlit-client"; const client = new Graphlit({ organizationId: process.env.GRAPHLIT_ORGANIZATION_ID, environmentId: process.env.GRAPHLIT_ENVIRONMENT_ID, jwtSecret: process.env.GRAPHLIT_JWT_SECRET, }); ``` -------------------------------- ### Handle Insufficient Token Budget with Context Strategy Options Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/errors.md Illustrates a scenario where the token budget might be exceeded due to large tool results or many conversation rounds. Adjusting `contextStrategy` options like `toolResultTokenLimit` and `toolRoundLimit` can help mitigate this. ```typescript // Conversation with large tool results and many rounds await client.streamAgent( "Process all these documents...", handler, conversationId, { id: specificationId }, tools, toolHandlers, { contextStrategy: { toolResultTokenLimit: 100000, // Very large toolRoundLimit: 50, }, } ); // May fail if total tokens exceed model's limit ``` -------------------------------- ### Get Medical Therapy by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific medical therapy entity using its unique identifier. ```typescript await graphlit.getMedicalTherapy(id: string): Promise ``` -------------------------------- ### Get Persona by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Looks up a persona using its unique identifier. A correlation ID can be provided for tracking. ```typescript await graphlit.getPersona(id: string, correlationId?: string): Promise ``` -------------------------------- ### Graphlit Constructor Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/api-reference/graphlit-client.md Initializes a new Graphlit client instance. You can provide organization ID, environment ID, JWT secret, owner ID, user ID, and API URI directly or via an options object. Environment variables are used as fallbacks if parameters are not provided. ```APIDOC ## new Graphlit(organizationId?, environmentId?, jwtSecret?, ownerId?, userId?, apiUri?) ### Description Initializes a new Graphlit client instance. You can provide organization ID, environment ID, JWT secret, owner ID, user ID, and API URI directly or via an options object. Environment variables are used as fallbacks if parameters are not provided. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Constructor Signature ```typescript new Graphlit( organizationId?: string | GraphlitClientOptions, environmentId?: string, jwtSecret?: string, ownerId?: string, userId?: string, apiUri?: string ): Graphlit ``` ### Parameters Details - **organizationId** (string | GraphlitClientOptions) - Optional - Organization ID (GUID) or options object. Uses `GRAPHLIT_ORGANIZATION_ID` env var if not provided. - **environmentId** (string) - Optional - Environment ID (GUID). Uses `GRAPHLIT_ENVIRONMENT_ID` env var if not provided. - **jwtSecret** (string) - Optional - JWT secret for authentication. Uses `GRAPHLIT_JWT_SECRET` env var if not provided. - **ownerId** (string) - Optional - Optional owner ID for multi-tenant scenarios. Uses `GRAPHLIT_OWNER_ID` env var if provided. - **userId** (string) - Optional - Optional user ID for multi-tenant scenarios. Must be a valid GUID. Uses `GRAPHLIT_USER_ID` env var if provided. - **apiUri** (string) - Optional - GraphQL API endpoint. Uses `GRAPHLIT_API_URL` env var if provided. Defaults to `https://data-scus.graphlit.io/api/v1/graphql`. ### Returns A Graphlit client instance ready to make API calls. ### Throws Error if required credentials (organizationId, environmentId, jwtSecret) are missing and no pre-signed token is provided. Also throws if GUIDs are in an invalid format. ### Example ```typescript import { Graphlit, Types } from "graphlit-client"; // Method 1: Using environment variables const client = new Graphlit(); // Method 2: Explicit parameters const client = new Graphlit( "your-organization-id", "your-environment-id", "your-jwt-secret" ); // Method 3: Options object (recommended) const client = new Graphlit({ organizationId: "your-organization-id", environmentId: "your-environment-id", jwtSecret: "your-jwt-secret", retryConfig: { maxAttempts: 10, initialDelay: 500, maxDelay: 60000, }, }); // Method 4: With pre-signed token (jwtSecret not required) const client = new Graphlit({ organizationId: "your-organization-id", environmentId: "your-environment-id", token: "your-pre-signed-token", }); ``` ``` -------------------------------- ### countContents Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Counts the number of contents that match the provided filter criteria. This is useful for getting statistics on content. ```APIDOC ## countContents ### Description Counts contents based on the provided filter criteria. ### Method N/A (SDK Method) ### Endpoint N/A (SDK Method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **filter** (Types.ContentFilter) - Optional - Filter criteria for counting contents. ### Request Example ```typescript await graphlit.countContents(filter?: Types.ContentFilter) ``` ### Response #### Success Response - **countContents** (Types.CountContentsQuery) - The result of the countContents operation. ``` -------------------------------- ### Create Conversational AI Specification Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/README.md This snippet shows how to create a specification for a conversational AI model, configuring its behavior with a system prompt and specific OpenAI parameters like model and temperature. It then demonstrates how to use this specification to stream agent responses in a conversation. ```typescript import { Graphlit, Types } from "graphlit-client"; // Create a conversational AI specification const conversationSpec = await client.createSpecification({ name: "Customer Support AI", type: Types.SpecificationTypes.Completion, serviceType: Types.ModelServiceTypes.OpenAi, systemPrompt: "You are a helpful customer support assistant.", openAI: { model: Types.OpenAiModels.Gpt4O_128K, temperature: 0.7, completionTokenLimit: 2000, }, }); // Use the specification in conversations await client.streamAgent( "How do I reset my password?", (event) => { if (event.type === "message_update") { console.log(event.message.message); } }, undefined, { id: conversationSpec.createSpecification.id }, ); ``` -------------------------------- ### Count Contents Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Counts the number of contents that match the provided filter criteria. Useful for getting statistics on content. ```typescript await graphlit.countContents(filter?: Types.ContentFilter): Promise ``` -------------------------------- ### DateRangeFilter Type Definition Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Defines a filter for a range of dates, specifying a start ('from') and end ('to') point. ```typescript from?: InputMaybe; to?: InputMaybe; ``` -------------------------------- ### Create a View Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Use this to create a new view for content filtering. It takes a `ViewInput` object and returns a `CreateViewMutation`. ```typescript await graphlit.createView(view: Types.ViewInput): Promise ``` -------------------------------- ### runAgent() Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/INDEX.md Executes an agent with a given prompt and options, returning a structured result. ```APIDOC ## runAgent() ### Description Executes an agent with a specified prompt and configuration options. This method provides a structured way to interact with agents and retrieve detailed results, including metrics. ### Method `runAgent(prompt: string, options?: RunAgentOptions): Promise` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript async function runAgentTask() { try { const result = await client.runAgent("Summarize the provided document.", { // agent options if any }); console.log("Agent result:", result.output); console.log("Metrics:", result.metrics); } catch (error) { console.error("Error running agent:", error); } } runAgentTask(); ``` ### Response #### Success Response (200) Returns an `AgentResult` object containing the agent's output and performance metrics. #### Response Example ```json { "output": "The document discusses the impact of climate change...", "metrics": { "processingTime": 2500, "tokensGenerated": 150 } } ``` ``` -------------------------------- ### MentionReferenceInput Type Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Represents a reference to a mention, including start and end points. Uses 'observable' for the referenced entity. ```typescript end?: InputMaybe; observable?: InputMaybe; start?: InputMaybe; type?: InputMaybe; ``` -------------------------------- ### Set Custom OpenAI Client Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/_autodocs/configuration.md Integrate a custom OpenAI client for native streaming capabilities. Ensure the OpenAI SDK is imported and initialized with your API key. ```typescript const { OpenAI } = await import("openai"); client.setOpenAIClient(new OpenAI({ apiKey: process.env.OPENAI_API_KEY })); ``` -------------------------------- ### Execute Agent with Non-Streaming Response Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Calls the promptAgent function to get a non-streaming response from an agent. The response is available in `response.agentResult`. ```typescript await graphlit.promptAgent( prompt: string, conversationId?: string, specification?: Types.EntityReferenceInput, tools?: Types.ToolDefinitionInput[], toolHandlers?: Record, options?: AgentOptions, mimeType?: string, data?: string, // base64 encoded, contentFilter?: Types.ContentCriteriaInput, augmentedFilter?: Types.ContentCriteriaInput, correlationId?: string, persona?: Types.EntityReferenceInput, ): Promise ``` -------------------------------- ### queryGitHubRepositories Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Queries GitHub repositories. This method enables fetching a list of repositories from GitHub, with options for sorting. ```APIDOC ## queryGitHubRepositories ### Description Queries GitHub repositories. This method enables fetching a list of repositories from GitHub, with options for sorting. ### Method ```typescript await graphlit.queryGitHubRepositories(properties: Types.GitHubRepositoriesInput, sortBy?: Types.GitHubRepositorySortTypes): Promise ``` ### Parameters #### Request Body - **properties** (Types.GitHubRepositoriesInput) - Required - Input object for filtering GitHub repositories. #### Query Parameters - **sortBy** (Types.GitHubRepositorySortTypes) - Optional - Specifies the sorting order for the GitHub repositories. ``` -------------------------------- ### Get Investment by ID Source: https://github.com/graphlit/graphlit-client-typescript/blob/main/llms-full.md Retrieves a specific investment entity using its unique ID. A correlation ID can be provided for tracing. ```typescript await graphlit.getInvestment(id: string, correlationId?: string): Promise ```