Skip to content

Commit

Permalink
JS V3 (#1571)
Browse files Browse the repository at this point in the history
* Sync collections JS

* More documents

* Clean up messy code

* list not List

* Users first pass

* User tests and fixmes

* More

* typo

* More prompts

* Pre-commit improvements

* Remove prints

* Cleanups on conversations

* Branches response

* Chunks

* More work on the return types

* Jest config

* Fix branch creation time
  • Loading branch information
NolanTrem authored Nov 12, 2024
1 parent adc2d68 commit 668ac9a
Show file tree
Hide file tree
Showing 81 changed files with 4,164 additions and 1,820 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ repos:
- id: check-ast
- id: check-yaml

- repo: local
hooks:
- id: check-typing-imports
name: Check for Dict/List usage
entry: bash -c 'echo "Checking for typing imports..." && find . -name "*.py" | xargs grep -n "from typing.*import.*[^d]Dict\\|from typing.*import.*List" || exit 0 && echo "⚠️ Please import dict/list instead of Dict/List from typing" && exit 1'
language: system
types: [python]
pass_filenames: false

- repo: local
hooks:
- id: isort
Expand Down
95 changes: 95 additions & 0 deletions js/sdk/__tests__/ChunksIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { r2rClient } from "../src/index";
const fs = require("fs");
import { describe, test, beforeAll, expect } from "@jest/globals";

const baseUrl = "http://localhost:7272";

describe("r2rClient V3 Collections Integration Tests", () => {
let client: r2rClient;

beforeAll(async () => {
client = new r2rClient(baseUrl);
await client.users.login({
email: "admin@example.com",
password: "change_me_immediately",
});
});

test("Create a chunk", async () => {
const response = await client.chunks.create({
chunks: [
{
id: "a285d6ff-1219-4315-a7d4-649b300af992",
document_id: "a285d6ff-1219-4315-a7d4-649b300af992",
collection_ids: [],
metadata: { key: "value" },
text: "Hello, world!",
},
],
run_with_orchestration: false,
});

expect(response.results).toEqual([
{
document_id: expect.any(String),
message: "Ingestion task completed successfully.",
task_id: null,
},
]);
});

test("Retrieve a chunk", async () => {
const response = await client.chunks.retrieve({
id: "a285d6ff-1219-4315-a7d4-649b300af992",
});

expect(response.results).toMatchObject({
id: expect.any(String),
document_id: expect.any(String),
text: expect.any(String),
collection_ids: expect.any(Array),
metadata: expect.any(Object),
});
});

test("Update a chunk", async () => {
const response = await client.chunks.update({
id: "a285d6ff-1219-4315-a7d4-649b300af992",
text: "Hello, world! How are you?",
});

expect(response.results).toMatchObject({
id: expect.any(String),
document_id: expect.any(String),
text: "Hello, world! How are you?",
collection_ids: expect.any(Array),
metadata: expect.any(Object),
});
});

test("Retrieve a chunk after update and check text", async () => {
const response = await client.chunks.retrieve({
id: "a285d6ff-1219-4315-a7d4-649b300af992",
});

expect(response.results.text).toBe("Hello, world! How are you?");
});

test("List chunks", async () => {
const response = await client.chunks.list();
expect(response.results).toBeDefined();
});

test("Delete a chunk", async () => {
const response = await client.chunks.delete(
"a285d6ff-1219-4315-a7d4-649b300af992",
);
expect(response.results.success).toBe(true);
});

test("Delete a chunk that does not exist", async () => {
await expect(
client.chunks.delete("a285d6ff-1219-4315-a7d4-649b300af992"),
).rejects.toThrow(/Status 404/);
});
});
142 changes: 72 additions & 70 deletions js/sdk/__tests__/CollectionsIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,104 @@ import { describe, test, beforeAll, expect } from "@jest/globals";

const baseUrl = "http://localhost:7272";

/**
* Test Collection should have a UUID of `6f2a5494-f759-4f12-a7b6-db836f651577`
*/
describe("r2rClient V3 Collections Integration Tests", () => {
let client: r2rClient;
let collectionId: string;
let documentId: string;

beforeAll(async () => {
client = new r2rClient(baseUrl);
await client.login("admin@example.com", "change_me_immediately");
await client.users.login({
email: "admin@example.com",
password: "change_me_immediately",
});
});

test("Create new collection", async () => {
const response = await client.collections.create("Test Collection");
const response = await client.collections.create({
name: "Test Collection",
});
expect(response).toBeTruthy();
collectionId = response.results.collection_id; // Updated to use correct path
collectionId = response.results.collection_id;
});

test("Delete collection", async () => {
await expect(client.collections.delete(collectionId)).resolves.toBeTruthy();
test("List collections", async () => {
const response = await client.collections.list();
expect(response.results).toBeDefined();
});

// test("Create document with content", async () => {
// const response = await client.documents.create({
// content: "This is a test document",
// metadata: { title: "Test Document" },
// });

// expect(response.results.document_id).toBeDefined();
// });

// test("Update document", async () => {
// const response = await client.documents.update({
// id: documentId,
// content: "Updated content",
// metadata: { title: "Updated Test Document" },
// });
test("Retrieve collection", async () => {
const response = await client.collections.retrieve({ id: collectionId });
expect(response.results).toBeDefined();
});

// expect(response.results).toBeDefined();
// });
test("Update collection", async () => {
const response = await client.collections.update({
id: collectionId,
name: "Updated Test Collection",
});
expect(response.results).toBeDefined();
});

// test("Retrieve document", async () => {
// const response = await client.documents.retrieve(documentId);
test("Ingest document and assign to collection", async () => {
const ingestResponse = await client.documents.create({
file: { path: "examples/data/zametov.txt", name: "zametov.txt" },
metadata: { title: "zametov.txt" },
});

// expect(response.results).toBeDefined();
// expect(response.results.id).toBe(documentId);
// });
expect(ingestResponse.results.document_id).toBeDefined();
documentId = ingestResponse.results.document_id;

// test("List documents with no parameters", async () => {
// const response = await client.documents.list();
const response = await client.collections.addDocument({
id: collectionId,
documentId: documentId,
});

// expect(response.results).toBeDefined();
// expect(Array.isArray(response.results)).toBe(true);
// });
expect(response.results).toBeDefined();
});

// test("List documents with parameters", async () => {
// const response = await client.documents.list({
// offset: 0,
// limit: 5,
// });
test("List documents in collection", async () => {
const response = await client.collections.listDocuments({
id: collectionId,
});
expect(response.results).toBeDefined();
});

// expect(response.results).toBeDefined();
// expect(Array.isArray(response.results)).toBe(true);
// expect(response.results.length).toBeLessThanOrEqual(5);
// TODO: Need to implement user methods in V3
// test("Add user to collection", async () => {
// const response = await client.collections.addUser({
// id: collectionId,
// userId: "",
// });
// expect(response.results).toBeDefined
// });

// test("Error handling - Create document with no file or content", async () => {
// await expect(
// client.documents.create({
// metadata: { title: "No Content" },
// }),
// ).rejects.toThrow(/Either file.*or content must be provided/);
// });
test("List users in collection", async () => {
const response = await client.collections.listUsers({ id: collectionId });
expect(response.results).toBeDefined();
});

// test("Error handling - Create document with both file and content", async () => {
// await expect(
// client.documents.create({
// file: {
// path: "examples/data/raskolnikov.txt",
// name: "raskolnikov.txt",
// },
// content: "Test content",
// metadata: { title: "Both File and Content" },
// }),
// ).rejects.toThrow(/Cannot provide both file.*and content/);
// TODO: Need to implement user methods in V3
// test("Remove user from collection", async () => {
// const response = await client.collections.removeUser({
// id: collectionId,
// userId: "",
// });
// expect(response.results).toBeDefined();
// });

// test("Delete Raskolnikov.txt", async () => {
// const response = await client.documents.delete("f9f61fc8-079c-52d0-910a-c657958e385b");

// expect(response.results).toBeDefined();
// });
test("Remove document from collection", async () => {
const response = await client.collections.removeDocument({
id: collectionId,
documentId: documentId,
});

// test("Delete untitled document", async () => {
// const response = await client.documents.delete("5556836e-a51c-57c7-916a-de76c79df2b6");
expect(response.results).toBeDefined();
});

// expect(response.results).toBeDefined();
// });
test("Delete collection", async () => {
await expect(
client.collections.delete({ id: collectionId }),
).resolves.toBeTruthy();
});
});
62 changes: 62 additions & 0 deletions js/sdk/__tests__/ConversationsIntegrationSuperUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { r2rClient } from "../src/index";
const fs = require("fs");
import { describe, test, beforeAll, expect } from "@jest/globals";

const baseUrl = "http://localhost:7272";

describe("r2rClient V3 Collections Integration Tests", () => {
let client: r2rClient;
let conversationId: string;
let messageId: string;

beforeAll(async () => {
client = new r2rClient(baseUrl);
await client.users.login({
email: "admin@example.com",
password: "change_me_immediately",
});
});

test("List all conversations", async () => {
const response = await client.conversations.list();
expect(response.results).toBeDefined();
});

test("Create a conversation", async () => {
const response = await client.conversations.create();
conversationId = response.results.id;
expect(response.results).toBeDefined();
});

test("Add a message to a conversation", async () => {
const response = await client.conversations.addMessage({
id: conversationId,
content: "Hello, world!",
role: "user",
});
messageId = response.results.id;
expect(response.results).toBeDefined();
});

// TODO: This is throwing a 405? Why?
// test("Update a message in a conversation", async () => {
// const response = await client.conversations.updateMessage({
// id: conversationId,
// message_id: messageId,
// content: "Hello, world! How are you?",
// });
// expect(response.results).toBeDefined();
// });

test("List branches in a conversation", async () => {
const response = await client.conversations.listBranches({
id: conversationId,
});
expect(response.results).toBeDefined();
});

test("Delete a conversation", async () => {
const response = await client.conversations.delete({ id: conversationId });
expect(response.results).toBeDefined();
});
});
Loading

0 comments on commit 668ac9a

Please sign in to comment.