Skip to content

Improvements on typing #279

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

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: 1 addition & 1 deletion src/collections/generate/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
GenerativeOpenAIConfigRuntime,
} from '../index.js';

export const generativeConfigRuntime = {
export const generativeParameters = {
/**
* Create a `ModuleConfig<'generative-anthropic', GenerativeConfigRuntimeType<'generative-anthropic'> | undefined>` object for use when performing runtime-specific AI generation using the `generative-anthropic` module.
*
Expand Down
2 changes: 1 addition & 1 deletion src/collections/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,5 @@ class GenerateManager<T> implements Generate<T> {

export default GenerateManager.use;

export { generativeConfigRuntime } from './config.js';
export { generativeParameters } from './config.js';
export { Generate } from './types.js';
5 changes: 3 additions & 2 deletions src/collections/generate/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
import { WeaviateUnsupportedFeatureError } from '../../errors.js';
import weaviate, { WeaviateClient, generativeConfigRuntime } from '../../index.js';
import weaviate, { WeaviateClient } from '../../index.js';
import { Collection } from '../collection/index.js';
import { GenerateOptions, GroupByOptions } from '../types/index.js';
import { generativeParameters } from './config.js';

const maybe = process.env.OPENAI_APIKEY ? describe : describe.skip;

Expand Down Expand Up @@ -493,7 +494,7 @@ maybe('Testing of the collection.generate methods with runtime generative config
nonBlobProperties: ['testProp'],
metadata: true,
},
config: generativeConfigRuntime.openAI({
config: generativeParameters.openAI({
stop: ['\n'],
}),
});
Expand Down
169 changes: 169 additions & 0 deletions src/collections/generate/mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import express from 'express';
import { Server as HttpServer } from 'http';
import { Server as GrpcServer, createServer } from 'nice-grpc';
import weaviate, { Collection, GenerativeConfigRuntime, WeaviateClient } from '../..';
import {
HealthCheckRequest,
HealthCheckResponse,
HealthCheckResponse_ServingStatus,
HealthDefinition,
HealthServiceImplementation,
} from '../../proto/google/health/v1/health';
import { GenerativeResult } from '../../proto/v1/generative';
import { SearchReply, SearchRequest, SearchResult } from '../../proto/v1/search_get';
import { WeaviateDefinition, WeaviateServiceImplementation } from '../../proto/v1/weaviate';
import { generativeParameters } from './config';

const mockedSingleGenerative = 'Mocked single response';
const mockedGroupedGenerative = 'Mocked group response';

class GenerateMock {
private grpc: GrpcServer;
private http: HttpServer;

constructor(grpc: GrpcServer, http: HttpServer) {
this.grpc = grpc;
this.http = http;
}

public static use = async (version: string, httpPort: number, grpcPort: number) => {
const httpApp = express();
// Meta endpoint required for client instantiation
httpApp.get('/v1/meta', (req, res) => res.send({ version }));

// gRPC health check required for client instantiation
const healthMockImpl: HealthServiceImplementation = {
check: (request: HealthCheckRequest): Promise<HealthCheckResponse> =>
Promise.resolve(HealthCheckResponse.create({ status: HealthCheckResponse_ServingStatus.SERVING })),
watch: jest.fn(),
};

const grpc = createServer();
grpc.add(HealthDefinition, healthMockImpl);

// Search endpoint returning generative mock data
const weaviateMockImpl: WeaviateServiceImplementation = {
aggregate: jest.fn(),
tenantsGet: jest.fn(),
search: (req: SearchRequest): Promise<SearchReply> => {
expect(req.generative?.grouped?.queries.length).toBeGreaterThan(0);
expect(req.generative?.single?.queries.length).toBeGreaterThan(0);
return Promise.resolve(
SearchReply.fromPartial({
results: [
SearchResult.fromPartial({
properties: {
nonRefProps: { fields: { name: { textValue: 'thing' } } },
},
generative: GenerativeResult.fromPartial({
values: [
{
result: mockedSingleGenerative,
},
],
}),
metadata: {
id: 'b602a271-d5a9-4324-921d-5abe4748d6b5',
},
}),
],
generativeGroupedResults: GenerativeResult.fromPartial({
values: [
{
result: mockedGroupedGenerative,
},
],
}),
})
);
},
batchDelete: jest.fn(),
batchObjects: jest.fn(),
};
grpc.add(WeaviateDefinition, weaviateMockImpl);

await grpc.listen(`localhost:${grpcPort}`);
const http = await httpApp.listen(httpPort);
return new GenerateMock(grpc, http);
};

public close = () => Promise.all([this.http.close(), this.grpc.shutdown()]);
}

describe('Mock testing of generate with runtime config', () => {
let client: WeaviateClient;
let collection: Collection;
let mock: GenerateMock;

beforeAll(async () => {
mock = await GenerateMock.use('1.30.0-rc.1', 8958, 8959);
client = await weaviate.connectToLocal({ port: 8958, grpcPort: 8959 });
collection = client.collections.use('Whatever');
});

afterAll(() => mock.close());

const stringTest = (config: GenerativeConfigRuntime) =>
collection.generate
.fetchObjects({
singlePrompt: 'What is the meaning of life?',
groupedTask: 'What is the meaning of life?',
config: config,
})
.then((res) => {
expect(res.generative?.text).toEqual(mockedGroupedGenerative);
expect(res.objects[0].generative?.text).toEqual(mockedSingleGenerative);
});

const objectTest = (config: GenerativeConfigRuntime) =>
collection.generate
.fetchObjects({
singlePrompt: {
prompt: 'What is the meaning of life?',
},
groupedTask: {
prompt: 'What is the meaning of life?',
},
config: config,
})
.then((res) => {
expect(res.generative?.text).toEqual(mockedGroupedGenerative);
expect(res.objects[0].generative?.text).toEqual(mockedSingleGenerative);
});

const model = { model: 'llama-2' };

const tests: GenerativeConfigRuntime[] = [
generativeParameters.anthropic(),
generativeParameters.anthropic(model),
generativeParameters.anyscale(),
generativeParameters.anyscale(model),
generativeParameters.aws(),
generativeParameters.aws(model),
generativeParameters.azureOpenAI(),
generativeParameters.azureOpenAI(model),
generativeParameters.cohere(),
generativeParameters.cohere(model),
generativeParameters.databricks(),
generativeParameters.databricks(model),
generativeParameters.friendliai(),
generativeParameters.friendliai(model),
generativeParameters.google(),
generativeParameters.google(model),
generativeParameters.mistral(),
generativeParameters.mistral(model),
generativeParameters.nvidia(),
generativeParameters.nvidia(model),
generativeParameters.ollama(),
generativeParameters.ollama(model),
generativeParameters.openAI(),
generativeParameters.openAI(model),
];

tests.forEach((conf) => {
it(`should get the mocked response for ${conf.name} with config: ${conf.config}`, async () => {
await stringTest(conf);
await objectTest(conf);
});
});
});
44 changes: 22 additions & 22 deletions src/collections/generate/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GenerativeConfigRuntimeType, ModuleConfig } from '../types';
import { generativeConfigRuntime } from './config';
import { generativeParameters } from './config';

// only tests fields that must be mapped from some public name to a gRPC name, e.g. baseURL -> baseUrl and stop: string[] -> stop: TextArray
describe('Unit testing of the generativeConfigRuntime factory methods', () => {
describe('Unit testing of the generativeParameters factory methods', () => {
describe('anthropic', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.anthropic();
const config = generativeParameters.anthropic();
expect(config).toEqual<
ModuleConfig<'generative-anthropic', GenerativeConfigRuntimeType<'generative-anthropic'> | undefined>
>({
Expand All @@ -14,7 +14,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.anthropic({
const config = generativeParameters.anthropic({
baseURL: 'http://localhost:8080',
stopSequences: ['a', 'b', 'c'],
});
Expand All @@ -32,7 +32,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('anyscale', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.anyscale();
const config = generativeParameters.anyscale();
expect(config).toEqual<
ModuleConfig<'generative-anyscale', GenerativeConfigRuntimeType<'generative-anyscale'> | undefined>
>({
Expand All @@ -41,7 +41,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.anyscale({
const config = generativeParameters.anyscale({
baseURL: 'http://localhost:8080',
});
expect(config).toEqual<
Expand All @@ -57,7 +57,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('aws', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.aws();
const config = generativeParameters.aws();
expect(config).toEqual<
ModuleConfig<'generative-aws', GenerativeConfigRuntimeType<'generative-aws'> | undefined>
>({
Expand All @@ -69,7 +69,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('azure-openai', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.azureOpenAI();
const config = generativeParameters.azureOpenAI();
expect(config).toEqual<
ModuleConfig<'generative-azure-openai', GenerativeConfigRuntimeType<'generative-azure-openai'>>
>({
Expand All @@ -78,7 +78,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.azureOpenAI({
const config = generativeParameters.azureOpenAI({
baseURL: 'http://localhost:8080',
model: 'model',
stop: ['a', 'b', 'c'],
Expand All @@ -99,7 +99,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('cohere', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.cohere();
const config = generativeParameters.cohere();
expect(config).toEqual<
ModuleConfig<'generative-cohere', GenerativeConfigRuntimeType<'generative-cohere'> | undefined>
>({
Expand All @@ -108,7 +108,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.cohere({
const config = generativeParameters.cohere({
baseURL: 'http://localhost:8080',
stopSequences: ['a', 'b', 'c'],
});
Expand All @@ -126,7 +126,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('databricks', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.databricks();
const config = generativeParameters.databricks();
expect(config).toEqual<
ModuleConfig<
'generative-databricks',
Expand All @@ -138,7 +138,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.databricks({
const config = generativeParameters.databricks({
stop: ['a', 'b', 'c'],
});
expect(config).toEqual<
Expand All @@ -157,7 +157,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('friendliai', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.friendliai();
const config = generativeParameters.friendliai();
expect(config).toEqual<
ModuleConfig<
'generative-friendliai',
Expand All @@ -169,7 +169,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.friendliai({
const config = generativeParameters.friendliai({
baseURL: 'http://localhost:8080',
});
expect(config).toEqual<
Expand All @@ -188,7 +188,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('mistral', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.mistral();
const config = generativeParameters.mistral();
expect(config).toEqual<
ModuleConfig<'generative-mistral', GenerativeConfigRuntimeType<'generative-mistral'> | undefined>
>({
Expand All @@ -197,7 +197,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.mistral({
const config = generativeParameters.mistral({
baseURL: 'http://localhost:8080',
});
expect(config).toEqual<
Expand All @@ -213,7 +213,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('nvidia', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.nvidia();
const config = generativeParameters.nvidia();
expect(config).toEqual<
ModuleConfig<'generative-nvidia', GenerativeConfigRuntimeType<'generative-nvidia'> | undefined>
>({
Expand All @@ -222,7 +222,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.nvidia({
const config = generativeParameters.nvidia({
baseURL: 'http://localhost:8080',
});
expect(config).toEqual<
Expand All @@ -238,7 +238,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('ollama', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.ollama();
const config = generativeParameters.ollama();
expect(config).toEqual<
ModuleConfig<'generative-ollama', GenerativeConfigRuntimeType<'generative-ollama'> | undefined>
>({
Expand All @@ -250,7 +250,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {

describe('openai', () => {
it('with defaults', () => {
const config = generativeConfigRuntime.openAI();
const config = generativeParameters.openAI();
expect(config).toEqual<
ModuleConfig<'generative-openai', GenerativeConfigRuntimeType<'generative-openai'>>
>({
Expand All @@ -259,7 +259,7 @@ describe('Unit testing of the generativeConfigRuntime factory methods', () => {
});
});
it('with values', () => {
const config = generativeConfigRuntime.openAI({
const config = generativeParameters.openAI({
baseURL: 'http://localhost:8080',
model: 'model',
stop: ['a', 'b', 'c'],
Expand Down
Loading
Loading