Skip to content

Commit

Permalink
Add user agent to node client (#170)
Browse files Browse the repository at this point in the history
* saving

* Use user agent

* Clean up tests

* Remove testing delays
  • Loading branch information
calebjohn24 authored Dec 10, 2024
1 parent fc49f31 commit 2771af1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 178 deletions.
35 changes: 35 additions & 0 deletions clients/node/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Source files
src/

# Test files
test/
*.test.ts
*.spec.ts

# Configuration files
tsconfig.json
.eslintrc*
.prettierrc*
jest.config.*

# Development files
.git/
.github/
.gitignore
.npmignore

# IDE files
.vscode/
.idea/

# Logs and debugging
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Misc
coverage/
docs/
examples/
.DS_Store
193 changes: 20 additions & 173 deletions clients/node/src/__tests__/moondream.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('MoondreamClient Integration Tests', () => {

describe('caption', () => {
it('should get a caption for a real image', async () => {

const request: CaptionRequest = {
image: imageBuffer,
length: 'normal',
Expand All @@ -40,181 +41,20 @@ describe('MoondreamClient Integration Tests', () => {
expect(result.caption).toBeDefined();
expect(typeof result.caption).toBe('string');
console.log('Caption:', result.caption);
}, 10000); // Increased timeout for API call
}, 100000); // Increased timeout for API call

it('should stream captions for a real image', async () => {

const request: CaptionRequest = {
image: imageBuffer,
length: 'normal',
stream: true
};
const result = await client.caption(request);

// Handle both streaming and non-streaming responses
if (typeof result.caption === 'string') {
expect(result.caption).toBeTruthy();
console.log('Caption (non-streamed):', result.caption);
} else {
const chunks: string[] = [];
for await (const chunk of result.caption) {
chunks.push(chunk);
}
const finalCaption = chunks.join('');
expect(finalCaption).toBeTruthy();
expect(chunks.length).toBeGreaterThan(0);
console.log('Streamed caption:', finalCaption);
}
}, 10000);
});

describe('caption-no-stream', () => {
it('should get a caption for a real image', async () => {
const request: CaptionRequest = {
image: imageBuffer,
length: 'normal',
stream: false
};
const result = await client.caption(request);
expect(result.caption).toBeDefined();
expect(typeof result.caption).toBe('string');
console.log('Caption:', result.caption);
expect((result.caption as string).length).toBeGreaterThan(0);
}, 10000);
});

describe('query', () => {
it('should answer questions about a real image', async () => {
const question = "What colors are present in this image?";
const request: QueryRequest = {
image: imageBuffer,
question: question,
stream: false
};
const result = await client.query(request);

expect(result.answer).toBeDefined();
expect(typeof result.answer).toBe('string');
console.log('Question:', question);
console.log('Answer:', result.answer);
}, 10000);

it('should stream answers about a real image', async () => {
const question = "What is the character doing?";
const request: QueryRequest = {
const result = await client.caption({
image: imageBuffer,
question: question,
length: 'short',
stream: true
};
const result = await client.query(request);

// Handle both streaming and non-streaming responses
if (typeof result.answer === 'string') {
expect(result.answer).toBeTruthy();
console.log('Question:', question);
console.log('Answer (non-streamed):', result.answer);
} else {
const chunks: string[] = [];
for await (const chunk of result.answer) {
chunks.push(chunk);
}
const finalAnswer = chunks.join('');
expect(finalAnswer).toBeTruthy();
expect(chunks.length).toBeGreaterThan(0);
console.log('Question:', question);
console.log('Streamed answer:', finalAnswer);
}
}, 10000);
});

describe('query-no-stream', () => {
it('should answer questions about a real image', async () => {
const question = "What colors are present in this image?";
const request: QueryRequest = {
image: imageBuffer,
question: question,
stream: false
};
const result = await client.query(request);
expect(result.answer).toBeDefined();
expect(typeof result.answer).toBe('string');
console.log('Answer:', result.answer);
});
});

describe('detect', () => {
it('should detect objects in a real image', async () => {
const objectToDetect = "burger";
const request: DetectRequest = {
image: imageBuffer,
object: objectToDetect,
};
const result = await client.detect(request);

expect(result.objects).toBeDefined();
expect(Array.isArray(result.objects)).toBe(true);
console.log('Detected objects:', result.objects);
}, 10000);
});

describe('point', () => {
it('should point to objects in a real image', async () => {
const objectToPoint = "burger";
const request: PointRequest = {
image: imageBuffer,
object: objectToPoint,
};
const result = await client.point(request);

expect(result.points).toBeDefined();
expect(Array.isArray(result.points)).toBe(true);
result.points.forEach(point => {
expect(point).toHaveProperty('x');
expect(point).toHaveProperty('y');
expect(typeof point.x).toBe('number');
expect(typeof point.y).toBe('number');
});
console.log('Pointed locations:', result.points);
}, 10000);
});
});

describe('MoondreamClient Local Server Integration Tests', () => {
let client: vl;
let imageBuffer: Base64EncodedImage;

const moondreamConfig: MoondreamVLConfig = {
apiUrl: 'http://localhost:3475'
};

beforeAll(async () => {
client = new vl(moondreamConfig);
// Load test image and convert to base64
const rawBuffer = await fs.readFile(path.join(__dirname, '../../../../assets/demo-1.jpg'));
imageBuffer = {
imageUrl: rawBuffer.toString('base64')
};
});

describe('caption', () => {
it('should get a caption for a real image', async () => {
const request: CaptionRequest = {
image: imageBuffer,
length: 'normal',
stream: false
};
const result = await client.caption(request);
expect(result.caption).toBeDefined();
expect(typeof result.caption).toBe('string');
console.log('Caption:', result.caption);
}, 10000); // Increased timeout for API call

it('should stream captions for a real image', async () => {
const request: CaptionRequest = {
image: imageBuffer,
length: 'normal',
stream: true
};
const result = await client.caption(request);

// Handle both streaming and non-streaming responses
if (typeof result.caption === 'string') {
Expand All @@ -230,26 +70,29 @@ describe('MoondreamClient Local Server Integration Tests', () => {
expect(chunks.length).toBeGreaterThan(0);
console.log('Streamed caption:', finalCaption);
}
}, 10000);
}, 100000);
});

describe('caption-no-stream', () => {
it('should get a caption for a real image', async () => {

const request: CaptionRequest = {
image: imageBuffer,
length: 'normal',
length: 'short',
stream: false
};
const result = await client.caption(request);

expect(result.caption).toBeDefined();
expect(typeof result.caption).toBe('string');
console.log('Caption:', result.caption);
expect((result.caption as string).length).toBeGreaterThan(0);
}, 10000);
}, 100000);
});

describe('query', () => {
it('should answer questions about a real image', async () => {

const question = "What colors are present in this image?";
const request: QueryRequest = {
image: imageBuffer,
Expand All @@ -262,9 +105,10 @@ describe('MoondreamClient Local Server Integration Tests', () => {
expect(typeof result.answer).toBe('string');
console.log('Question:', question);
console.log('Answer:', result.answer);
}, 10000);
}, 100000);

it('should stream answers about a real image', async () => {

const question = "What is the character doing?";
const request: QueryRequest = {
image: imageBuffer,
Expand All @@ -289,11 +133,12 @@ describe('MoondreamClient Local Server Integration Tests', () => {
console.log('Question:', question);
console.log('Streamed answer:', finalAnswer);
}
}, 10000);
}, 100000);
});

describe('query-no-stream', () => {
it('should answer questions about a real image', async () => {

const question = "What colors are present in this image?";
const request: QueryRequest = {
image: imageBuffer,
Expand All @@ -304,11 +149,12 @@ describe('MoondreamClient Local Server Integration Tests', () => {
expect(result.answer).toBeDefined();
expect(typeof result.answer).toBe('string');
console.log('Answer:', result.answer);
});
}, 100000);
});

describe('detect', () => {
it('should detect objects in a real image', async () => {

const objectToDetect = "burger";
const request: DetectRequest = {
image: imageBuffer,
Expand All @@ -319,11 +165,12 @@ describe('MoondreamClient Local Server Integration Tests', () => {
expect(result.objects).toBeDefined();
expect(Array.isArray(result.objects)).toBe(true);
console.log('Detected objects:', result.objects);
}, 10000);
}, 100000);
});

describe('point', () => {
it('should point to objects in a real image', async () => {

const objectToPoint = "burger";
const request: PointRequest = {
image: imageBuffer,
Expand All @@ -340,6 +187,6 @@ describe('MoondreamClient Local Server Integration Tests', () => {
expect(typeof point.y).toBe('number');
});
console.log('Pointed locations:', result.points);
}, 10000);
}, 100000);
});
});
12 changes: 8 additions & 4 deletions clients/node/src/__tests__/moondream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ describe('MoondreamClient', () => {
method: 'POST',
headers: {
'X-Moondream-Auth': mockApiKey,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'User-Agent': expect.stringMatching(/^moondream-node\/\d+\.\d+\.\d+$/)
}
})
);
Expand Down Expand Up @@ -141,7 +142,8 @@ describe('MoondreamClient', () => {
method: 'POST',
headers: {
'X-Moondream-Auth': mockApiKey,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'User-Agent': expect.stringMatching(/^moondream-node\/\d+\.\d+\.\d+$/)
}
})
);
Expand Down Expand Up @@ -217,7 +219,8 @@ describe('MoondreamClient', () => {
method: 'POST',
headers: {
'X-Moondream-Auth': mockApiKey,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'User-Agent': expect.stringMatching(/^moondream-node\/\d+\.\d+\.\d+$/)
}
})
);
Expand Down Expand Up @@ -355,7 +358,8 @@ describe('MoondreamClient', () => {
method: 'POST',
headers: {
'X-Moondream-Auth': mockApiKey,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'User-Agent': expect.stringMatching(/^moondream-node\/\d+\.\d+\.\d+$/)
},
body: expect.stringContaining('dog')
})
Expand Down
Loading

0 comments on commit 2771af1

Please sign in to comment.