Skip to content

Commit

Permalink
v0.3.32
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Apr 2, 2024
1 parent 72dc7c2 commit bbd4a73
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@premai/prem-sdk",
"version": "0.3.31",
"version": "0.3.32",
"license": "MIT",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { Axios, AxiosError, AxiosRequestConfig, CreateAxiosDefaults } from "axios"
import { PremBaseConfig } from "$types/index"

import apiModule from "$modules/api"
import chatcompletionsModule from "$modules/chat.completions"
import datapointsModule from "$modules/datapoints"
import embeddingsModule from "$modules/embeddings"
Expand All @@ -12,7 +13,8 @@ export default class Prem {
config: PremBaseConfig & { baseUrl: string }
axios: Axios

chat = { completions: new chatcompletionsModule(this) }
api = new apiModule(this)
chat = { completions: new chatcompletionsModule(this) }
datapoints = new datapointsModule(this)
embeddings = new embeddingsModule(this)
finetuning = new finetuningModule(this)
Expand Down
7 changes: 7 additions & 0 deletions src/modules/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { APIModule } from "$components/index"

import api_projects_generate_description_retrieve from "./operations/api_projects_generate_description_retrieve"

export default class extends APIModule {
retrieve = api_projects_generate_description_retrieve(this.client)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AxiosRequestConfig } from "axios"
import Prem from "$src/index"
import type { operations } from "$types/api"

export default (client: Prem) => (id: string, options?: AxiosRequestConfig): Promise<operations["api_projects_generate_description_retrieve"]["responses"]["200"]["content"]["application/json"]> => {
return client.call({
method: "get",
url: `/api/projects/${id}/generate-description/`,

...options
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
} from "$types/index"


type CreateChatCompletionOut<T> = T extends true ? AsyncIterable<ChatCompletionStreamingCompletionData> & {
type CreateChatCompletionOut<T> = T extends true ? AsyncIterable<ChatCompletionStreamingCompletionData> & {
trace_id: string | null,
document_chunks: null | DocumentChunk[]
} : CreateChatCompletionResponse
Expand All @@ -36,19 +36,48 @@ export default (client: Prem) => async<T extends boolean>(

const parsedEventEmitter = new EventEmitter()

let buffer = ''

rawStream.on("data", (chunk: Buffer) => {
const { data, event } = parseEventString(chunk.toString());
if (event === "completion" && data !== null) {
parsedEventEmitter.emit("data", data)
} else if (event === "done") {
parsedEventEmitter.emit("trace", data.traceId)
parsedEventEmitter.emit("document_chunks", data.documentChunks)
parsedEventEmitter.emit("end")
buffer += chunk.toString()
let splitPattern = /\n\nevent:/g
let events = buffer.split(splitPattern)

if (events.length > 1) {
events.slice(0, -1).forEach(eventString => {
eventString = eventString.startsWith('event:') ? eventString : 'event:' + eventString
const { data, event } = parseEventString(eventString)

if (event === "completion" && data !== null) {
parsedEventEmitter.emit("data", data)
} else if (event === "done") {
parsedEventEmitter.emit("trace", data.traceId)
parsedEventEmitter.emit("document_chunks", data.documentChunks)
parsedEventEmitter.emit("end")
}
})

buffer = events[events.length - 1]
}
})

// Add an 'end' listener to handle any remaining data in the buffer.
rawStream.on("end", () => {
parsedEventEmitter.emit("end")
// Check if there is any remanent data that constitutes a complete event.
if (buffer.trim()) {
// Ensure the buffer is treated as a complete event for parsing.
if (!buffer.startsWith('event:')) {
buffer = 'event:' + buffer
}
const { data, event } = parseEventString(buffer)
if (event === "completion" && data !== null) {
parsedEventEmitter.emit("data", data)
} else if (event === "done") {
parsedEventEmitter.emit("trace", data.traceId)
parsedEventEmitter.emit("document_chunks", data.documentChunks)
parsedEventEmitter.emit("end")
}
}
})

const iterable = eventEmitterToAsyncIterable<ChatCompletionStreamingCompletionData>(parsedEventEmitter)
Expand Down
16 changes: 16 additions & 0 deletions src/types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> &
type OneOf<T extends any[]> = T extends [infer Only] ? Only : T extends [infer A, infer B, ...infer Rest] ? OneOf<[XOR<A, B>, ...Rest]> : never;

export interface paths {
"/api/projects/{id}/generate-description/": {
get: operations["api_projects_generate_description_retrieve"];
};
"/v1/chat/completions": {
/** @description Creates a model response for the given chat conversation. */
post: operations["v1_chat_completions_create"];
Expand Down Expand Up @@ -723,6 +726,19 @@ export type external = Record<string, never>;

export interface operations {

api_projects_generate_description_retrieve: {
parameters: {
path: {
id: number;
};
};
responses: {
/** @description No response body */
200: {
content: never;
};
};
};
/** @description Creates a model response for the given chat conversation. */
v1_chat_completions_create: {
requestBody: {
Expand Down

0 comments on commit bbd4a73

Please sign in to comment.