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: OpenAPI spec update via Stainless API #14

Merged
merged 1 commit into from
Mar 8, 2024
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
4 changes: 1 addition & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ If you’d like to use the repository from source, you can either install from g
To install via git:

```bash
npm install --save git+ssh://[email protected]:groq/groq-typescript.git
# or
yarn add git+ssh://[email protected]:groq/groq-typescript.git
npm install git+ssh://[email protected]:groq/groq-typescript.git
```

Alternatively, to link a local copy of the repo:
Expand Down
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ The REST API documentation can be found [on console.groq.com](https://console.gr
## Installation

```sh
# install from NPM
npm install --save groq-sdk
# or
yarn add groq-sdk
npm install groq-sdk
```

## Usage
Expand Down Expand Up @@ -80,7 +77,7 @@ async function main() {
],
model: 'mixtral-8x7b-32768',
})
.catch((err) => {
.catch(async (err) => {
if (err instanceof Groq.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
Expand Down Expand Up @@ -233,18 +230,26 @@ If you would like to disable or customize this behavior, for example to use the
<!-- prettier-ignore -->
```ts
import http from 'http';
import HttpsProxyAgent from 'https-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';

// Configure the default for all requests:
const groq = new Groq({
httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),
});

// Override per-request:
await groq.chat.completions.create({ messages: [{ role: 'system', content: 'You are a helpful assisstant.' }, { role: 'user', content: 'Explain the importance of low latency LLMs' }], model: 'mixtral-8x7b-32768' }, {
baseURL: 'http://localhost:8080/test-api',
httpAgent: new http.Agent({ keepAlive: false }),
})
await groq.chat.completions.create(
{
messages: [
{ role: 'system', content: 'You are a helpful assisstant.' },
{ role: 'user', content: 'Explain the importance of low latency LLMs' },
],
model: 'mixtral-8x7b-32768',
},
{
httpAgent: new http.Agent({ keepAlive: false }),
},
);
```

## Semantic Versioning
Expand Down
16 changes: 0 additions & 16 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { VERSION } from './version';
import { Stream } from './lib/streaming';
import {
GroqError,
APIError,
Expand Down Expand Up @@ -39,19 +38,6 @@ type APIResponseProps = {

async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
const { response } = props;
if (props.options.stream) {
debug('response', response.status, response.url, response.headers, response.body);

// Note: there is an invariant here that isn't represented in the type system
// that if you set `stream: true` the response type must also be `Stream<T>`

if (props.options.__streamClass) {
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
}

return Stream.fromSSEResponse(response, props.controller) as any;
}

// fetch refuses to read the body when the status code is 204.
if (response.status === 204) {
return null as T;
Expand Down Expand Up @@ -750,7 +736,6 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
idempotencyKey?: string;

__binaryResponse?: boolean | undefined;
__streamClass?: typeof Stream;
};

// This is required so that we can determine if a given object matches the RequestOptions
Expand All @@ -771,7 +756,6 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
idempotencyKey: true,

__binaryResponse: true,
__streamClass: true,
};

export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/chat_completions_ext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Manually curated models for streaming chat completions.
import { ChatCompletion } from '../resources/chat'
import { ChatCompletion } from '../resources/chat';

export interface ChatCompletionChunk {
id: string;
Expand Down Expand Up @@ -74,7 +74,7 @@ export namespace ChatCompletionChunk {
id?: string;
usage?: ChatCompletion.Usage;
error?: string;
}
};
}

export interface ChatCompletionTokenLogprob {
Expand Down
37 changes: 3 additions & 34 deletions src/resources/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,13 @@
import * as Core from 'groq-sdk/core';
import { APIResource } from 'groq-sdk/resource';
import * as CompletionsAPI from 'groq-sdk/resources/chat/completions';
import { Stream } from 'groq-sdk/lib/streaming';
import { ChatCompletionChunk } from 'groq-sdk/lib/chat_completions_ext';

export class Completions extends APIResource {
/**
* Creates a completion for a chat prompt
*/
create(
body: ChatCompletionCreateParamsNonStreaming,
options?: Core.RequestOptions,
): Core.APIPromise<ChatCompletion>;
create(
body: ChatCompletionCreateParamsStreaming,
options?: Core.RequestOptions,
): Core.APIPromise<Stream<ChatCompletionChunk>>;
create(
body: ChatCompletionCreateParamsBase,
options?: Core.RequestOptions,
): Core.APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>;
create(
body: ChatCompletionCreateParams,
options?: Core.RequestOptions,
): Core.APIPromise<ChatCompletion> | Core.APIPromise<Stream<ChatCompletionChunk>> {
return this._client.post('/openai/v1/chat/completions', { body, ...options, stream: body.stream ?? false }) as
| Core.APIPromise<ChatCompletion>
| Core.APIPromise<Stream<ChatCompletionChunk>>;
create(body: CompletionCreateParams, options?: Core.RequestOptions): Core.APIPromise<ChatCompletion> {
return this._client.post('/openai/v1/chat/completions', { body, ...options });
}
}

Expand Down Expand Up @@ -130,7 +111,7 @@ export namespace ChatCompletion {
}
}

export interface ChatCompletionCreateParamsBase {
export interface CompletionCreateParams {
messages: Array<CompletionCreateParams.Message>;

model: string;
Expand Down Expand Up @@ -254,15 +235,3 @@ export namespace Completions {
export import ChatCompletion = CompletionsAPI.ChatCompletion;
export import CompletionCreateParams = CompletionsAPI.CompletionCreateParams;
}

export interface ChatCompletionCreateParamsNonStreaming extends ChatCompletionCreateParamsBase {
stream?: false;
}

export interface ChatCompletionCreateParamsStreaming extends ChatCompletionCreateParamsBase {
stream: true;
}

export type ChatCompletionCreateParams =
| ChatCompletionCreateParamsNonStreaming
| ChatCompletionCreateParamsStreaming;