Skip to content

Commit

Permalink
Update to use Projects Syntax (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
vowelparrot authored Jun 23, 2023
1 parent 5d3b121 commit 125bf5a
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 273 deletions.
6 changes: 3 additions & 3 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Tracing can be activated by setting the following environment variables or by ma
process.env["LANGCHAIN_TRACING_V2"] = "true";
process.env["LANGCHAIN_ENDPOINT"] = "https://api.langchain.plus"; // or your own server
process.env["LANGCHAIN_API_KEY"] = "<YOUR-LANGCHAINPLUS-API-KEY>";
// process.env["LANGCHAIN_SESSION"] = "My Session Name"; // Optional: "default" is used if not set
// process.env["LANGCHAIN_PROJECT"] = "My Session Name"; // Optional: "default" is used if not set
```

> **Tip:** Sessions are groups of traces. All runs are logged to a session. If not specified, the session is set to `default`.
Expand Down Expand Up @@ -80,7 +80,7 @@ or by directly specifying the connection information in the RunTree.
```typescript
process.env["LANGCHAIN_ENDPOINT"] = "https://api.langchain.plus"; // or your own server
process.env["LANGCHAIN_API_KEY"] = "<YOUR-LANGCHAINPLUS-API-KEY>";
// process.env["LANGCHAIN_SESSION"] = "My Session Name"; // Optional: "default" is used if not set
// process.env["LANGCHAIN_PROJECT"] = "My Session Name"; // Optional: "default" is used if not set
```

2. **Log traces using a RunTree.**
Expand All @@ -103,7 +103,7 @@ const parentRunConfig: RunTreeConfig = {
text: "Summarize this morning's meetings.",
},
serialized: {}, // Serialized representation of this chain
// session_name: "Defaults to the LANGCHAIN_SESSION env var"
// session_name: "Defaults to the LANGCHAIN_PROJECT env var"
// apiUrl: "Defaults to the LANGCHAIN_ENDPOINT env var"
// apiKey: "Defaults to the LANGCHAIN_API_KEY env var"
};
Expand Down
98 changes: 49 additions & 49 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ interface LangChainPlusClientConfig {
}

interface ListRunsParams {
sessionId?: string;
sessionName?: string;
projectId?: string;
projectName?: string;
executionOrder?: number;
runType?: RunType;
error?: boolean;
Expand Down Expand Up @@ -237,8 +237,8 @@ export class LangChainPlusClient {
}

public async listRuns({
sessionId,
sessionName,
projectId,
projectName,
executionOrder,
runType,
error,
Expand All @@ -247,15 +247,15 @@ export class LangChainPlusClient {
offset,
}: ListRunsParams): Promise<Run[]> {
const queryParams = new URLSearchParams();
let sessionId_ = sessionId;
if (sessionName) {
if (sessionId) {
throw new Error("Only one of sessionId or sessionName may be given");
let projectId_ = projectId;
if (projectName) {
if (projectId) {
throw new Error("Only one of projectId or projectName may be given");
}
sessionId_ = (await this.readSession({ sessionName })).id;
projectId_ = (await this.readProject({ projectName })).id;
}
if (sessionId_) {
queryParams.append("session", sessionId_);
if (projectId_) {
queryParams.append("session", projectId_);
}
if (executionOrder) {
queryParams.append("execution_order", executionOrder.toString());
Expand All @@ -281,24 +281,24 @@ export class LangChainPlusClient {
return this._get<Run[]>("/runs", queryParams);
}

public async createSession({
sessionName,
sessionExtra,
public async createProject({
projectName,
projectExtra,
mode,
upsert,
}: {
sessionName: string;
sessionExtra?: object;
projectName: string;
projectExtra?: object;
mode?: string;
upsert?: boolean;
}): Promise<TracerSession> {
const upsert_ = upsert ? `?upsert=true` : "";
const endpoint = `${this.apiUrl}/sessions${upsert_}`;
const body: Record<string, object | string> = {
name: sessionName,
name: projectName,
};
if (sessionExtra !== undefined) {
body["extra"] = sessionExtra;
if (projectExtra !== undefined) {
body["extra"] = projectExtra;
}
if (mode !== undefined) {
body["mode"] = mode;
Expand All @@ -312,29 +312,29 @@ export class LangChainPlusClient {
const result = await response.json();
if (!response.ok) {
throw new Error(
`Failed to create session ${sessionName}: ${response.status} ${response.statusText}`
`Failed to create session ${projectName}: ${response.status} ${response.statusText}`
);
}
return result as TracerSession;
}

public async readSession({
sessionId,
sessionName,
public async readProject({
projectId,
projectName,
}: {
sessionId?: string;
sessionName?: string;
projectId?: string;
projectName?: string;
}): Promise<TracerSessionResult> {
let path = "/sessions";
const params = new URLSearchParams();
if (sessionId !== undefined && sessionName !== undefined) {
throw new Error("Must provide either sessionName or sessionId, not both");
} else if (sessionId !== undefined) {
path += `/${sessionId}`;
} else if (sessionName !== undefined) {
params.append("name", sessionName);
if (projectId !== undefined && projectName !== undefined) {
throw new Error("Must provide either projectName or projectId, not both");
} else if (projectId !== undefined) {
path += `/${projectId}`;
} else if (projectName !== undefined) {
params.append("name", projectName);
} else {
throw new Error("Must provide sessionName or sessionId");
throw new Error("Must provide projectName or projectId");
}

const response = await this._get<TracerSession | TracerSession[]>(
Expand All @@ -345,7 +345,7 @@ export class LangChainPlusClient {
if (Array.isArray(response)) {
if (response.length === 0) {
throw new Error(
`Session[id=${sessionId}, name=${sessionName}] not found`
`Project[id=${projectId}, name=${projectName}] not found`
);
}
result = response[0] as TracerSessionResult;
Expand All @@ -355,30 +355,30 @@ export class LangChainPlusClient {
return result;
}

public async listSessions(): Promise<TracerSession[]> {
public async listProjects(): Promise<TracerSession[]> {
return this._get<TracerSession[]>("/sessions");
}

public async deleteSession({
sessionId,
sessionName,
public async deleteProject({
projectId,
projectName,
}: {
sessionId?: string;
sessionName?: string;
projectId?: string;
projectName?: string;
}): Promise<void> {
let sessionId_: string | undefined;
if (sessionId === undefined && sessionName === undefined) {
throw new Error("Must provide sessionName or sessionId");
} else if (sessionId !== undefined && sessionName !== undefined) {
throw new Error("Must provide either sessionName or sessionId, not both");
} else if (sessionId === undefined) {
sessionId_ = (await this.readSession({ sessionName })).id;
let projectId_: string | undefined;
if (projectId === undefined && projectName === undefined) {
throw new Error("Must provide projectName or projectId");
} else if (projectId !== undefined && projectName !== undefined) {
throw new Error("Must provide either projectName or projectId, not both");
} else if (projectId === undefined) {
projectId_ = (await this.readProject({ projectName })).id;
} else {
sessionId_ = sessionId;
projectId_ = projectId;
}
const response = await this.caller.call(
fetch,
`${this.apiUrl}/sessions/${sessionId_}`,
`${this.apiUrl}/sessions/${projectId_}`,
{
method: "DELETE",
headers: this.headers,
Expand All @@ -387,7 +387,7 @@ export class LangChainPlusClient {
);
await raiseForStatus(
response,
`delete session ${sessionId_} (${sessionName})`
`delete session ${projectId_} (${projectName})`
);
}

Expand Down
5 changes: 4 additions & 1 deletion js/src/run_trees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export class RunTree implements BaseRun {
private static getDefaultConfig(): object {
return {
id: uuid.v4(),
session_name: getEnvironmentVariable("LANGCHAIN_SESSION") ?? "default",
session_name:
getEnvironmentVariable("LANGCHAIN_PROJECT") ??
getEnvironmentVariable("LANGCHAIN_SESSION") ?? // TODO: Deprecate
"default",
child_runs: [],
execution_order: 1,
child_execution_order: 1,
Expand Down
2 changes: 2 additions & 0 deletions js/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface TracerSession {
mode?: string;
}

// Fully loaded information about a Tracer Session (also known
// as a Project)
export interface TracerSessionResult extends TracerSession {
// The number of runs in the session.
run_count?: number;
Expand Down
Loading

0 comments on commit 125bf5a

Please sign in to comment.