Skip to content

Commit

Permalink
add user-agent header for all fetch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
capaj authored and rychlis committed Apr 17, 2024
1 parent 7422268 commit e8716c9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
6 changes: 3 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Full API reference
# API reference

## LangtailNode

Expand All @@ -25,7 +25,7 @@ The constructor accepts an options object with the following properties:
This method accepts two parameters:

- `body`: An object that can be of type `ChatCompletionCreateParamsNonStreaming & ILangtailExtraProps`, `ChatCompletionCreateParamsStreaming & ILangtailExtraProps`, `ChatCompletionCreateParamsBase & ILangtailExtraProps`, or `ChatCompletionCreateParams & ILangtailExtraProps`.
- `options`: An optional `Core.RequestOptions` object.
- `options`(optional): OpenAI `Core.RequestOptions` object

It returns a promise that resolves to a `ChatCompletion` or a `Stream<ChatCompletionChunk>` depending whether you are using streaming or not.

Expand Down Expand Up @@ -62,7 +62,7 @@ This method accepts two parameters:

It returns a string representing the URL path for the prompt.

#### request
#### invoke

This method accepts an `IRequestParams` or `IRequestParamsStream` object and returns a promise that resolves to an `OpenAIResponseWithHttp` or a `StreamResponseType` depending on whether you use streaming or not.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const rawCompletion = await lt.chat.completions.create({
Completion from a deployed prompt can be called with `lt.prompts.invoke`:

```ts
const deployedPrompCompletion = await lt.prompts.invoke({
const deployedPromptCompletion = await lt.prompts.invoke({
prompt: "<PROMPT_SLUG>", // required
environment: "staging",
variables: {
Expand Down Expand Up @@ -96,7 +96,7 @@ which is necessary if your API key is workspace wide. For a project api key this
both chat.prompts.create and prompts.invoke support streaming responses. All you need to enable it is `{ stream: true }` flag like this:

```ts
const deployedPrompCompletion = await lt.prompts.invoke({
const deployedPromptCompletion = await lt.prompts.invoke({
prompt: "<PROMPT_SLUG>",
environment: "staging",
stream: true, // changes result to be a streaming OpenAI response
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langtail",
"version": "0.2.1",
"version": "0.2.2",
"description": "",
"main": "./dist/LangtailNode.js",
"packageManager": "[email protected]",
Expand Down
3 changes: 3 additions & 0 deletions src/LangtailNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from "openai/resources/chat/completions"
import { APIPromise } from "openai/core"
import { Stream } from "openai/src/streaming"
import { userAgent } from "./userAgent"


export const baseURL = "https://proxy.langtail.com/v1"

Expand Down Expand Up @@ -97,6 +99,7 @@ export class LangtailNode {
if (params.doNotRecord) {
options.headers = {
["x-langtail-do-not-record"]: "true",
'user-agent': userAgent,
...options?.headers,
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/LangtailPrompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ChatCompletionChunk } from "openai/resources/chat/completions"
import { Stream } from "openai/streaming"
import { ILangtailExtraProps } from "./LangtailNode"
import { Fetch } from "openai/core"
import { userAgent } from "./userAgent"

export type Environment = "preview" | "staging" | "production"

Expand Down Expand Up @@ -104,6 +105,7 @@ export class LangtailPrompts {
method: "POST",
headers: {
"X-API-Key": this.apiKey,
'user-agent': userAgent,
"content-type": "application/json",
"x-langtail-do-not-record": doNotRecord ? "true" : "false",
...metadataHeaders,
Expand Down
3 changes: 3 additions & 0 deletions src/userAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import packageJson from "../package.json"

export const userAgent = `langtail-js-sdk:${packageJson.version}`

0 comments on commit e8716c9

Please sign in to comment.