Skip to content

Commit

Permalink
Add version info to user-agent header (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Jan 16, 2024
1 parent 4ab0353 commit e0a9f2f
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/js_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@ jobs:
run: yarn install --immutable
- name: Build
run: yarn run build
- name: Check version
run: yarn run check-version
- name: Test
run: yarn run test
2 changes: 2 additions & 0 deletions .github/workflows/release_js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
run: cd js && yarn install --immutable
- name: Build
run: cd js && yarn run build
- name: Check version
run: yarn run check-version
- name: Publish package to NPM
run: |
cd js
Expand Down
6 changes: 4 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.0.59",
"version": "0.0.60",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"files": [
"dist/",
Expand Down Expand Up @@ -28,6 +28,8 @@
"types": "./dist/index.d.ts",
"scripts": {
"build": "yarn clean && yarn build:esm && yarn build:cjs && node scripts/create-entrypoints.js && node scripts/create-cli.js",
"bump-version": "node scripts/bump-version.js",
"check-version": "node scripts/check-version.js",
"clean": "rm -rf dist/ && node scripts/create-entrypoints.js clean",
"build:esm": "tsc --outDir dist/ && rm -rf dist/tests dist/**/tests",
"build:cjs": "tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rm -r dist-cjs",
Expand Down Expand Up @@ -119,4 +121,4 @@
},
"./package.json": "./package.json"
}
}
}
21 changes: 21 additions & 0 deletions js/scripts/bump-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { readFileSync, writeFileSync } from 'fs';
import process from 'process';
const packageJson = JSON.parse(readFileSync('package.json'));

let newVersion;
if (process.argv.length > 2) {
newVersion = process.argv[2];
} else {
const versionParts = packageJson.version.split('.');
versionParts[2] = parseInt(versionParts[2]) + 1;
newVersion = versionParts.join('.');
}
console.log(`Bumping version to ${newVersion}`);

packageJson.version = newVersion;
writeFileSync('package.json', JSON.stringify(packageJson, null, 2));

const indexFilePath = 'src/index.ts';
let indexFileContent = readFileSync(indexFilePath, 'utf-8');
indexFileContent = indexFileContent.replace(/export const __version__ = "[0-9]+\.[0-9]+\.[0-9]+";/g, `export const __version__ = "${newVersion}";`);
writeFileSync(indexFilePath, indexFileContent);
17 changes: 17 additions & 0 deletions js/scripts/check-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readFileSync } from "fs";
import { join } from "path";
const indexFilePath = "src/index.ts";
const packageJson = JSON.parse(readFileSync("package.json"));
let indexFileContent = readFileSync(indexFilePath, "utf-8");

const packageVersion = packageJson.version;
const indexVersion = indexFileContent.match(
/__version__\s*=\s*['"]([^'"]+)['"]/
)[1];

if (packageVersion !== indexVersion) {
throw new Error(
`Version mismatch! package.json version: ${packageVersion}, index.ts version: ${indexVersion}`
);
}
console.log(`Version check passed: ${packageVersion} === ${indexVersion}`);
5 changes: 4 additions & 1 deletion js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { getEnvironmentVariable, getRuntimeEnvironment } from "./utils/env.js";

import { RunEvaluator } from "./evaluation/evaluator.js";
import { __version__ } from "./index.js";

interface ClientConfig {
apiUrl?: string;
Expand Down Expand Up @@ -243,7 +244,9 @@ export class Client {
}

private get headers(): { [header: string]: string } {
const headers: { [header: string]: string } = {};
const headers: { [header: string]: string } = {
"User-Agent": `langsmith-js/${__version__}`,
};
if (this.apiKey) {
headers["x-api-key"] = `${this.apiKey}`;
}
Expand Down
3 changes: 3 additions & 0 deletions js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ export { Client } from "./client.js";
export { Dataset, Example, TracerSession, Run, Feedback } from "./schemas.js";

export { RunTree, RunTreeConfig } from "./run_trees.js";

// Update using yarn bump-version
export const __version__ = "0.0.60";
26 changes: 17 additions & 9 deletions js/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Inlined from https://github.com/flexdinesh/browser-or-node
import { __version__ } from "../index.js";
declare global {
const Deno:
| {
Expand All @@ -9,6 +10,8 @@ declare global {
| undefined;
}

let globalEnv: string;

export const isBrowser = () =>
typeof window !== "undefined" && typeof window.document !== "undefined";

Expand All @@ -35,27 +38,31 @@ export const isNode = () =>
!isDeno();

export const getEnv = () => {
let env: string;
if (globalEnv) {
return globalEnv;
}
if (isBrowser()) {
env = "browser";
globalEnv = "browser";
} else if (isNode()) {
env = "node";
globalEnv = "node";
} else if (isWebWorker()) {
env = "webworker";
globalEnv = "webworker";
} else if (isJsDom()) {
env = "jsdom";
globalEnv = "jsdom";
} else if (isDeno()) {
env = "deno";
globalEnv = "deno";
} else {
env = "other";
globalEnv = "other";
}

return env;
return globalEnv;
};

export type RuntimeEnvironment = {
library: string;
libraryVersion?: string;
sdk: string;
sdk_version: string;
runtime: string;
runtimeVersion?: string;
};
Expand All @@ -66,10 +73,11 @@ export async function getRuntimeEnvironment(): Promise<RuntimeEnvironment> {
if (runtimeEnvironment === undefined) {
const env = getEnv();
const releaseEnv = getShas();

runtimeEnvironment = {
library: "langsmith",
runtime: env,
sdk: "langsmith-js",
sdk_version: __version__,
...releaseEnv,
};
}
Expand Down
3 changes: 2 additions & 1 deletion python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from requests import adapters as requests_adapters
from urllib3.util import Retry

import langsmith
from langsmith import env as ls_env
from langsmith import schemas as ls_schemas
from langsmith import utils as ls_utils
Expand Down Expand Up @@ -362,7 +363,7 @@ def _headers(self) -> Dict[str, str]:
Dict[str, str]
The headers for the API request.
"""
headers = {}
headers = {"User-Agent": f"langsmith-py/{langsmith.__version__}"}
if self.api_key:
headers["x-api-key"] = self.api_key
return headers
Expand Down
1 change: 1 addition & 0 deletions python/langsmith/env/_runtime_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def get_runtime_environment() -> dict:

shas = get_release_shas()
return {
"sdk": "langsmith-py",
"sdk_version": __version__,
"library": "langsmith",
"platform": platform.platform(),
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langsmith"
version = "0.0.80"
version = "0.0.81"
description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform."
authors = ["LangChain <[email protected]>"]
license = "MIT"
Expand Down
5 changes: 3 additions & 2 deletions python/tests/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def test_validate_api_key_if_hosted(monkeypatch: pytest.MonkeyPatch) -> None:
def test_headers(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("LANGCHAIN_API_KEY", raising=False)
client = Client(api_url="http://localhost:1984", api_key="123")
assert client._headers == {"x-api-key": "123"}
assert "x-api-key" in client._headers
assert client._headers["x-api-key"] == "123"

client_no_key = Client(api_url="http://localhost:1984")
assert client_no_key._headers == {}
assert "x-api-key" not in client_no_key._headers


@mock.patch("langsmith.client.requests.Session")
Expand Down

0 comments on commit e0a9f2f

Please sign in to comment.