Installation
Use NPM or your package manager of choice to install thelangtail package:
Usage
Here are the different ways you can use the SDK.1. Invoke a prompt that’s deployed on Langtail (recommended)
To invoke a deployed prompt, you can uselt.prompts.invoke like this:
Invoke a prompt
2. Invoke a prompt that’s stored in code
If you’re storing your prompt in your codebase and want to give Langtail a try, use this method. You can uselt.chat.completions.create as a light wrapper over the OpenAI API that still sends logs and metrics to Langtail.
For more, see the example in this doc.
3. Fetch prompt from Langtail and invoke directly (proxyless)
If you’re storing your prompt in Langtail but want to invoke it directly from your code without using Langtail as a proxy, use this method. You can callLangtailPrompts.get to retrieve the contents of the prompt:
Fetch prompt from Langtail
Prompt response
Build the final output
OpenAI body
Call OpenAI SDK
API reference
LangtailNode
Constructor
The constructor accepts an options object with the following properties:string
required
The API key for Langtail.
string
The base URL for the Langtail API.
boolean
A boolean indicating whether to record the API calls.
string
The organization ID.
string
The project ID.
Function
The fetch function to use for making HTTP requests. It is passed to openAI
client under the
hood.
Properties
Object
An object containing a
completions object with a create method.LangtailPrompts
An instance of the
LangtailPrompts class.Methods
chat.completions.create
This method accepts two parameters:Object
required
An object that can be of type
ChatCompletionCreateParamsNonStreaming & ILangtailExtraProps, ChatCompletionCreateParamsStreaming & ILangtailExtraProps, ChatCompletionCreateParamsBase & ILangtailExtraProps,
or ChatCompletionCreateParams & ILangtailExtraProps.Core.RequestOptions
OpenAI
Core.RequestOptions object (optional).ChatCompletion or a Stream<ChatCompletionChunk> depending whether you are using streaming or not.
Create a chat completion
Exceptions
- Throws an error if the
apiKeyis not provided in the options object or as an environment variable.
LangtailPrompts
Constructor
The constructor accepts an options object with the following properties:string
required
The API key for Langtail.
string
The base URL for the Langtail API.
string
The organization ID.
string
The project ID.
Function
The fetch function to use for making HTTP requests. It is passed to openAI
client under the
hood.
Properties
string
required
The API key for Langtail.
string
The base URL for the Langtail API.
Object
An object containing the options for the Langtail API.
Methods
invoke
This method accepts:IRequestParams | IRequestParamsStream
required
An
IRequestParams or IRequestParamsStream object.OpenAIResponseWithHttp or a StreamResponseType depending on whether you use streaming or not.
Invoke a prompt
get
This method accepts one parameter with these fields:string
required
A string representing the prompt.
string
An
Environment string identifier. Accepts values: "preview" | "staging" | "production". Defaults to productionstring
String for version. Necessary for preview environment.
Fetch prompt from Langtail
build
This method accepts two parameters:PromptState
required
The state object returned from the
get method.Object
An object containing the options for the Langtail API.
Build the final output
Exceptions
- Throws an error if the fetch operation fails.
- Throws an error if there is no body in the response when streaming is enabled.