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/prompt error #19

Merged
merged 3 commits into from
Aug 29, 2023
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
5 changes: 5 additions & 0 deletions .changeset/loud-wasps-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'outpostkit': patch
---

added prompt response type and error handling
32 changes: 28 additions & 4 deletions src/comet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import type {
ProvideMessageFeedbackPayload,
IComet,
ListSessionsPayload,
ICometSession,
GetSessionPayload,
TCometPromptResponse,
ICometSession
} from './types';
import { streamPromptWithAxios, streamPromptWithNativeFetch } from 'helpers';

Expand Down Expand Up @@ -38,11 +40,33 @@ export class Comet implements IComet {
//TODO: better error handling
if (payload.stream) {
if (typeof window !== 'undefined') {
return await streamPromptWithNativeFetch(this.cometId, this.apiKey, payload, handleNewText);
} else return await streamPromptWithAxios(this.cometAPI, payload, handleNewText);
const resp = await streamPromptWithNativeFetch(
this.cometId,
this.apiKey,
payload,
handleNewText
);

// @ts-ignore
if (resp.error) {
// @ts-ignore
throw new Error(resp.error);
} else {
return resp as TCometPromptResponse;
}
} else {
const resp = await streamPromptWithAxios(this.cometAPI, payload, handleNewText);
// @ts-ignore
if (resp.error) {
// @ts-ignore
throw new Error(resp.error);
} else {
return resp as TCometPromptResponse;
}
}
} else {
const { data } = await this.cometAPI.post(`/prompt`, payload);
return data;
return data as TCometPromptResponse;
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AxiosInstance } from 'axios';
import { PROMPT_STREAM_RESPONSE_PREFIX } from './constants';
import { API_V1_URL } from './constants';
import { PromptPayload } from 'types';
import { PromptPayload, TCometPromptResponse } from 'types';
import { TCometPromptStreamResponseError } from './types';

export function sanitizeFilename(filename: string): string {
const splitFilename = filename.split('.');
Expand All @@ -18,7 +19,7 @@ export const streamPromptWithNativeFetch = (
payload: PromptPayload,
handleNewText?: (token: string) => void | Promise<void>
) => {
return new Promise<{ response: string; referencePaths: string[] }>((resolve, reject) => {
return new Promise<TCometPromptResponse | TCometPromptStreamResponseError>((resolve, reject) => {
(async () => {
const response = await fetch(`${API_V1_URL}/comets/${cometId}/prompt`, {
method: 'POST',
Expand Down Expand Up @@ -56,12 +57,7 @@ export const streamPromptWithNativeFetch = (
}
if (done) {
try {
return resolve(
JSON.parse(responseText) as {
response: string;
referencePaths: string[];
}
);
return resolve(JSON.parse(responseText));
} catch (e) {
return reject('Could not parse the response');
}
Expand All @@ -79,7 +75,7 @@ export const streamPromptWithAxios = (
payload: PromptPayload,
handleNewText?: (token: string) => void | Promise<void>
) => {
return new Promise((resolve, reject) => {
return new Promise<TCometPromptResponse | TCometPromptStreamResponseError>((resolve, reject) => {
(async () => {
const { data: stream } = await cometAPI.post(`/prompt`, payload, {
responseType: 'stream',
Expand All @@ -104,7 +100,7 @@ export const streamPromptWithAxios = (
});

stream.on('end', () => {
return resolve(responseText);
return resolve(JSON.parse(responseText));
});
})();
});
Expand Down
43 changes: 31 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// Memory Client
export interface MemoryClient {
addMemory: (sessionId: string, payload: Memory) => Promise<Memory>
getMemory: (sessionId: string) => Promise<Memory>
deleteMemory: (sessionId: string) => Promise<void>
addMemory: (sessionId: string, payload: Memory) => Promise<Memory>;
getMemory: (sessionId: string) => Promise<Memory>;
deleteMemory: (sessionId: string) => Promise<void>;
}

export interface MemoryConfig {
apiKey?: string
clientId?: string
baseUrl?: string
apiKey?: string;
clientId?: string;
baseUrl?: string;
}

export interface Memory {
messages: Array<{
content: string
role: 'Human' | 'AI'
}>
context?: string
content: string;
role: 'Human' | 'AI';
}>;
context?: string;
}

// Comet Client
export interface IComet {
prompt: (
payload: PromptPayload,
handleNewText?: (data: string) => void | Promise<void>
) => Promise<any>;
) => Promise<TCometPromptResponse>;
updateConfig: (payload: UpdateConfigPayload) => Promise<void>;
setGenerationModel: (payload: SetGenerationModelPayload) => Promise<void>;
getMessage: (payload: GetMessagePayload) => Promise<ICometMessage>;
Expand Down Expand Up @@ -254,4 +254,23 @@ export interface ICometMessage {
meta: Record<string, any> | null;
conversationId: string;
createdAt: string;
}
}

export type TCometPromptResponse = {
text: string;
referencePaths?: string[];
referencesWithSources?: {
path: string;
source_id: string;
}[];
usage: {
prompt_tokens: number;
completion_tokens: number;
};
conversationId?: string;
sessionId?: string;
};

export type TCometPromptStreamResponseError = {
error: string;
};