Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prompt): add url to getPrompt #64

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,7 @@ export class API {
variables
variablesDefaultValues
version
url
lineage {
name
}
Expand Down Expand Up @@ -2064,6 +2065,7 @@ export class API {
variables
variablesDefaultValues
version
url
lineage {
name
}
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type StoredContext = {
rootRun: Step | null;
};

const storage = new AsyncLocalStorage<StoredContext>();
/**
* The LiteralClient class provides an interface to interact with the Literal AI API.
* It offers methods for creating threads and steps, as well as instrumentation for various AI services.
Expand All @@ -40,7 +39,8 @@ export class LiteralClient {
api: API;
openai: ReturnType<typeof openai>;
instrumentation: ReturnType<typeof instrumentation>;
store: AsyncLocalStorage<StoredContext> = storage;
store: AsyncLocalStorage<StoredContext> =
new AsyncLocalStorage<StoredContext>();

/**
* Initialize a new Literal AI Client.
Expand Down Expand Up @@ -115,7 +115,7 @@ export class LiteralClient {
* @returns The current thread, if any.
*/
_currentThread(): Thread | null {
const store = storage.getStore();
const store = this.store.getStore();

return store?.currentThread || null;
}
Expand All @@ -125,7 +125,7 @@ export class LiteralClient {
* @returns The current step, if any.
*/
_currentStep(): Step | null {
const store = storage.getStore();
const store = this.store.getStore();

return store?.currentStep || null;
}
Expand All @@ -135,7 +135,7 @@ export class LiteralClient {
* @returns The current experiment, if any.
*/
_currentExperimentItemRunId(): string | null {
const store = storage.getStore();
const store = this.store.getStore();

return store?.currentExperimentItemRunId || null;
}
Expand All @@ -145,7 +145,7 @@ export class LiteralClient {
* @returns The root run, if any.
*/
_rootRun(): Step | null {
const store = storage.getStore();
const store = this.store.getStore();

return store?.rootRun || null;
}
Expand All @@ -156,7 +156,7 @@ export class LiteralClient {
* @returns The current thread, if any.
*/
getCurrentThread(): Thread {
const store = storage.getStore();
const store = this.store.getStore();

if (!store?.currentThread) {
throw new Error(
Expand All @@ -173,7 +173,7 @@ export class LiteralClient {
* @returns The current step, if any.
*/
getCurrentStep(): Step {
const store = storage.getStore();
const store = this.store.getStore();

if (!store?.currentStep) {
throw new Error(
Expand All @@ -190,7 +190,7 @@ export class LiteralClient {
* @returns The current experiment, if any.
*/
getCurrentExperimentItemRunId(): string {
const store = storage.getStore();
const store = this.store.getStore();

if (!store?.currentExperimentItemRunId) {
throw new Error(
Expand All @@ -207,7 +207,7 @@ export class LiteralClient {
* @returns The current step, if any.
*/
getRootRun(): Step {
const store = storage.getStore();
const store = this.store.getStore();

if (!store?.rootRun) {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions src/prompt-engineering/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class PromptFields extends Utils {
createdAt!: string;
name!: string;
version!: number;
url?: Maybe<string>;
versionDesc?: Maybe<string>;
metadata!: Record<string, any>;
items!: Array<OmitUtils<DatasetItem>>;
Expand Down
17 changes: 13 additions & 4 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe('End to end tests for the SDK', function () {
})
.send();

await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const fetchedStep = await client.api.getStep(step.id!);
expect(fetchedStep?.id).toBe(step.id);
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('End to end tests for the SDK', function () {
})
.send();

await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const fetchedStep = await client.api.getStep(step.id!);
expect(fetchedStep?.id).toBe(step.id);
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('End to end tests for the SDK', function () {

expect(step.id).not.toBeNull();

await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const steps = await client.api.getSteps({
filters: [
Expand Down Expand Up @@ -269,7 +269,7 @@ describe('End to end tests for the SDK', function () {
})
.send();

await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const score = await client.api.createScore({
stepId: step.id!,
Expand Down Expand Up @@ -597,6 +597,15 @@ describe('End to end tests for the SDK', function () {
expect(fetchedPrompt?.version).toBe(0);
});

it('should get the URL for the prompt', async () => {
const prompt = await client.api.getPrompt('Default', 0);
const projectId = await client.api.getProjectId();

expect(prompt?.url).toContain(
`projects/${projectId}/playground?name=Default&version=0`
);
});

it('should format a prompt with default values', async () => {
const prompt = await client.api.getPrompt('Default');

Expand Down
4 changes: 2 additions & 2 deletions tests/attachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Attachments', () => {
})
.send();

await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const fetchedStep = await client.api.getStep(step.id!);

Expand Down Expand Up @@ -86,7 +86,7 @@ describe('Attachments', () => {
});
});

await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const fetchedStep = await client.api.getStep(stepId!);

Expand Down
8 changes: 5 additions & 3 deletions tests/integration/openai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe('OpenAI Instrumentation', () => {
});
});

await new Promise((resolve) => setTimeout(resolve, 1000));
await new Promise((resolve) => setTimeout(resolve, 2000));

const {
data: [step]
Expand Down Expand Up @@ -318,6 +318,8 @@ describe('OpenAI Instrumentation', () => {
})
]);

await new Promise((resolve) => setTimeout(resolve, 3000));

const {
data: [firstGeneration]
} = await client.api.getSteps({
Expand Down Expand Up @@ -366,7 +368,7 @@ describe('OpenAI Instrumentation', () => {
});
});

await new Promise((resolve) => setTimeout(resolve, 2000));
await new Promise((resolve) => setTimeout(resolve, 4000));

const {
data: [step]
Expand Down Expand Up @@ -409,7 +411,7 @@ describe('OpenAI Instrumentation', () => {
});
});

await new Promise((resolve) => setTimeout(resolve, 3000));
await new Promise((resolve) => setTimeout(resolve, 5000));

const {
data: [step]
Expand Down
22 changes: 11 additions & 11 deletions tests/wrappers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Wrapper', () => {
});
});

await sleep(1000);
await sleep(2000);
const thread = await client.api.getThread(threadId!);
const step = await client.api.getStep(stepId!);

Expand Down Expand Up @@ -120,7 +120,7 @@ describe('Wrapper', () => {
});
});

await sleep(1000);
await sleep(2000);
const thread = await client.api.getThread(threadId!);
const run = await client.api.getStep(runId!);
const retrieveStep = await client.api.getStep(retrieveStepId!);
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('Wrapper', () => {
});
});

await sleep(1000);
await sleep(2000);
const run = await client.api.getStep(runId!);
const createdStep = await client.api.getStep(stepId!);

Expand All @@ -187,7 +187,7 @@ describe('Wrapper', () => {
});
});

await sleep(1000);
await sleep(2000);
const run = await client.api.getStep(runId!);
const step = await client.api.getStep(stepId!);

Expand Down Expand Up @@ -246,7 +246,7 @@ describe('Wrapper', () => {
{ metadata: { key: 'thread-value' } }
);

await sleep(1000);
await sleep(2000);
const thread = await client.api.getThread(threadId!);
const step = await client.api.getStep(stepId!);

Expand Down Expand Up @@ -279,7 +279,7 @@ describe('Wrapper', () => {
(output) => ({ metadata: { assistantMessage: output.content } })
);

await sleep(1000);
await sleep(2000);
const thread = await client.api.getThread(threadId!);
const step = await client.api.getStep(stepId!);

Expand Down Expand Up @@ -310,7 +310,7 @@ describe('Wrapper', () => {
});
});

await sleep(1000);
await sleep(2000);
const thread = await client.api.getThread(threadId!);
const step = await client.api.getStep(stepId!);

Expand Down Expand Up @@ -338,7 +338,7 @@ describe('Wrapper', () => {
});
});

await sleep(1000);
await sleep(2000);
const thread = await client.api.getThread(threadId!);
const step = await client.api.getStep(stepId!);

Expand All @@ -353,7 +353,7 @@ describe('Wrapper', () => {
.thread({ name: 'Test Wrappers Thread' })
.upsert();

await sleep(1000);
await sleep(2000);
const thread = await client.api.getThread(threadId);

const wrappedThreadId = await thread!.wrap(async () => {
Expand All @@ -368,7 +368,7 @@ describe('Wrapper', () => {
.run({ name: 'Test Wrappers Thread' })
.send();

await sleep(1000);
await sleep(2000);
const step = await client.api.getStep(stepId!);

const wrappedStepId = await step!.wrap(async () => {
Expand Down Expand Up @@ -406,7 +406,7 @@ describe('Wrapper', () => {
});
expect(persistedExperimentItem).toBeTruthy();

await sleep(1000);
await sleep(2000);

const experimentRunId = persistedExperimentItem!.experimentRunId;
expect(experimentRunId).toBeTruthy();
Expand Down
Loading