Skip to content

Commit

Permalink
support aimode
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen Truong committed Oct 2, 2023
1 parent 3605ea2 commit 0a6bb2f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 49 deletions.
4 changes: 1 addition & 3 deletions etc/chat-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export enum Environment {
//
// @internal
export interface InternalConfig {
// (undocumented)
aiMode?: string;
promptPackage?: ChatPrompt;
}

Expand Down Expand Up @@ -83,8 +83,6 @@ export interface MessageRequest {
conversationId?: string;
messages: Message[];
notes?: MessageNotes;
// @internal
promptPackage?: ChatPrompt;
}

// @public
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yext/chat-core",
"version": "0.7.0",
"version": "0.7.1",
"description": "Typescript Networking Library for the Yext Chat API",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.mjs",
Expand Down
16 changes: 8 additions & 8 deletions src/infra/ChatCoreImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
MessageRequest,
MessageResponse,
Endpoints,
ChatPrompt,
} from "../models";
import { ApiMessageRequest } from "../models/endpoints/MessageRequest";
import { ApiResponse } from "../models/http/ApiResponse";
Expand All @@ -25,22 +24,22 @@ export class ChatCoreImpl implements ChatCore {
private chatConfig: ChatConfig;
private httpService: HttpService;
private endpoints: Endpoints;
private promptPackage: ChatPrompt;
private internalConfig: InternalConfig;

constructor(chatConfig: ChatConfig, internalConfig?: InternalConfig) {
constructor(chatConfig: ChatConfig, internalConfig: InternalConfig = {}) {
this.chatConfig = chatConfig;
this.httpService = new HttpServiceImpl();
this.endpoints =
chatConfig.endpoints ?? EndpointsFactory.getEndpoints(this.chatConfig);
this.promptPackage = internalConfig?.promptPackage ?? "stable";
this.endpoints = chatConfig.endpoints ?? EndpointsFactory.getEndpoints(this.chatConfig);
this.internalConfig = internalConfig
}

async getNextMessage(request: MessageRequest): Promise<MessageResponse> {
const queryParams: QueryParams = { v: defaultApiVersion };
const body: ApiMessageRequest = {
...request,
version: this.chatConfig.version,
promptPackage: this.promptPackage,
promptPackage: this.internalConfig.promptPackage,
aiMode: this.internalConfig.aiMode
};
const rawResponse = await this.httpService.post(
this.endpoints.chat,
Expand Down Expand Up @@ -74,7 +73,8 @@ export class ChatCoreImpl implements ChatCore {
const body: ApiMessageRequest = {
...request,
version: this.chatConfig.version,
promptPackage: this.promptPackage,
promptPackage: this.internalConfig.promptPackage,
aiMode: this.internalConfig.aiMode
};
const rawResponse = await this.httpService.post(
this.endpoints.chatStream,
Expand Down
16 changes: 16 additions & 0 deletions src/models/InternalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,21 @@ export type ChatPrompt = "stable" | "nightly";
* @internal
*/
export interface InternalConfig {
/**
* promptPackage corresponds to the package of prompts which will be used
* to carry out the instruction steps. When set to "nightly", the bot will
* use the most recent updates to the prompts, which may include experimental
* changes.
* It is STRONGLY recommended to use the default "stable" for your bot.
*
* @remarks
* The set of prompts which will be used by the bot's instruction steps.
* Defaults to "stable", which is the set of tested and verified prompts.
*/
promptPackage?: ChatPrompt;
/**
* Modes to determine types of models to use when sending requests the bot's
* instruction steps.
*/
aiMode?: string;
}
17 changes: 3 additions & 14 deletions src/models/endpoints/MessageRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ export interface MessageRequest {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
context?: any;
/**
* promptPackage corresponds to the package of prompts which will be used
* to carry out the instruction steps. When set to "nightly", the bot will
* use the most recent updates to the prompts, which may include experimental
* changes.
* It is STRONGLY recommended to use the default "stable" for your bot.
*
* @remarks
* The set of prompts which will be used by the bot's instruction steps.
* Defaults to "stable", which is the set of tested and verified prompts.
* @internal
*/
promptPackage?: ChatPrompt;
}

/**
Expand All @@ -69,6 +56,8 @@ export interface ApiMessageRequest {
messages: Message[];
/** {@inheritDoc MessageNotes} */
notes?: MessageNotes;
/** {@inheritdoc MessageRequest.promptPackage} */
/** {@inheritDoc InternalConfig.promptPackage} */
promptPackage?: ChatPrompt;
/** {@inheritDoc InternalConfig.aiMode} */
aiMode?: string;
}
32 changes: 17 additions & 15 deletions test-node-cjs/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import http from "http";
import { ChatConfig, StreamEventName, provideChatCore } from "@yext/chat-core";
import { ChatConfig, InternalConfig, MessageRequest, StreamEventName, provideChatCoreInternal } from "@yext/chat-core";
import dotenv from "dotenv";

dotenv.config();
Expand All @@ -14,17 +14,21 @@ const config: ChatConfig = {
},
};

const internalConfig: InternalConfig = { /** for testing pursposes */}

const request: MessageRequest = {
messages: [
{
timestamp: "2023-05-17T19:21:21.915Z",
source: "USER",
text: "How do I send an email?",
}
]
}

async function stream(res: any) {
const chatCore = provideChatCore(config);
const stream = await chatCore.streamNextMessage({
messages: [
{
timestamp: "2023-05-17T19:21:21.915Z",
source: "USER",
text: "How do I send an email?",
},
],
});
const chatCore = provideChatCoreInternal(config, internalConfig);
const stream = await chatCore.streamNextMessage(request);
Object.values(StreamEventName).forEach((eventName) => {
stream.addEventListener(eventName, (event) => {
console.log(`${eventName}:`, event.data);
Expand All @@ -42,10 +46,8 @@ const server = http.createServer(async (req: any, res: any) => {
if (req.url === "/streaming") {
stream(res);
} else {
const chatCore = provideChatCore(config);
const reply = await chatCore.getNextMessage({
messages: [],
});
const chatCore = provideChatCoreInternal(config, internalConfig);
const reply = await chatCore.getNextMessage(request);
res.end(JSON.stringify(reply, null, 2));
}
});
Expand Down
2 changes: 1 addition & 1 deletion test-node-cjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions tests/ChatCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const mockedMessageRequest: MessageRequest = {
foo: "bar",
},
messages: [],
promptPackage: "stable",
};

const defaultConfig: ChatConfig = {
Expand Down Expand Up @@ -131,7 +130,7 @@ describe("URL and http request construction", () => {
foo: "bar",
},
messages: [],
promptPackage: "stable",

},
"my-api-key"
);
Expand All @@ -150,7 +149,6 @@ describe("URL and http request construction", () => {
foo: "bar",
},
messages: [],
promptPackage: "stable",
},
"my-api-key"
);
Expand All @@ -175,7 +173,6 @@ describe("URL and http request construction", () => {
foo: "bar",
},
messages: [],
promptPackage: "stable",
},
"my-api-key"
);
Expand All @@ -195,7 +192,6 @@ describe("URL and http request construction", () => {
foo: "bar",
},
messages: [],
promptPackage: "stable",
},
"my-api-key"
);
Expand Down

0 comments on commit 0a6bb2f

Please sign in to comment.