Skip to content

Commit

Permalink
Merge branch 'main' into abhishek/descriptionOperatorCodeEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimasand authored Jul 22, 2022
2 parents 492acec + c165d34 commit 17ad35b
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ The following are the functions available on the `PromptEngine` class and those

For more examples and insights into using the prompt-engine library, have a look at the [examples](https://github.com/microsoft/prompt-engine/tree/main/examples) folder

## YAML Representation
It can be useful to represent prompts as standalone files, versus code. This can allow easy swapping between different prompts, prompt versioning, and other advanced capabiliites. With this in mind, prompt-engine offers a way to represent prompts as YAML and to load that YAML into a prompt-engine class. See `examples/yaml-examples` for examples of YAML prompts and how they're loaded into prompt-engine.

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Expand Down
18 changes: 18 additions & 0 deletions examples/yaml-examples/chat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type: chat-engine
config:
model-config:
max-tokens: 1024
user-name: "Abhishek"
bot-name: "Bot"
description: "What is the possibility of an event happening?"
examples:
- input: "Roam around Mars"
response: "This will be possible in a couple years"
- input: "Drive a car"
response: "This is possible after you get a learner drivers license"
flow-reset-text: "Starting a new conversation"
dialog:
- input: "Drink water"
response: "Uhm...You don't do that 8 times a day?"
- input: "Walk on air"
response: "For that you'll need a special device"
15 changes: 15 additions & 0 deletions examples/yaml-examples/code.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type: code-engine
config:
model-config:
max-tokens: 1024
description: "Natural Language Commands to Math Code"
examples:
- input: "what's 10 plus 18"
response: "console.log(10 + 18)"
- input: "what's 10 times 18"
response: "console.log(10 * 18)"
dialog:
- input: "what's 18 divided by 10"
response: "console.log(10 / 18)"
- input: "what's 18 factorial 10"
response: "console.log(10 % 18)"
23 changes: 23 additions & 0 deletions examples/yaml-examples/general.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type: prompt-engine
config:
model-config:
max-tokens: 1024
description-prefix: ">>"
description-postfix: ""
newline-operator: "\n"
input-prefix: "Human:"
input-postfix: ""
output-prefix: "Bot:"
output-postfix: ""
description: "What is the possibility of an event happening?"
examples:
- input: "Roam around Mars"
response: "This will be possible in a couple years"
- input: "Drive a car"
response: "This is possible after you get a learner drivers license"
flow-reset-text: "Starting a new conversation"
dialog:
- input: "Drink water"
response: "Uhm...You don't do that 8 times a day?"
- input: "Walk on air"
response: "For that you'll need a special device"
31 changes: 31 additions & 0 deletions examples/yaml-examples/yaml-chat-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { ChatEngine } = require("./../../out/ChatEngine");
const { readFileSync } = require("fs");

const promptEngine = new ChatEngine();

const yamlConfig = readFileSync("./examples/yaml-examples/chat.yaml", "utf8");
promptEngine.loadYAML(yamlConfig);

console.log(promptEngine.buildContext("", true))

/* Output for this example is:
PROMPT
What is the possibility of an event happening?
Abhishek: Roam around Mars
Bot: This will be possible in a couple years
Abhishek: Drive a car
Bot: This is possible after you get a learner drivers license
Starting a new conversation
Abhishek: Drink water
Bot: Uhm...You don't do that 8 times a day?
Abhishek: Walk on air
Bot: For that you'll need a special device
*/
30 changes: 30 additions & 0 deletions examples/yaml-examples/yaml-code-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { CodeEngine } = require("./../../out/CodeEngine");
const { readFileSync } = require("fs");

const promptEngine = new CodeEngine();

const yamlConfig = readFileSync("./examples/yaml-examples/code.yaml", "utf8");
promptEngine.loadYAML(yamlConfig);

const prompt = promptEngine.buildPrompt("what's 18 to the power of 10");

console.log(prompt)

// Output for this example is:
//
// /* Natural Language Commands to Math Code */
//
// /* what's 10 plus 18 */
// console.log(10 + 18)
//
// /* what's 10 times 18 */
// console.log(10 * 18)
//
// /* what's 18 divided by 10 */
// console.log(10 / 18)
//
// /* what's 18 factorial 10 */
// console.log(10 % 18)
//
// /* what's 18 to the power of 10 */

29 changes: 29 additions & 0 deletions examples/yaml-examples/yaml-general-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { PromptEngine } = require("./../../out/PromptEngine");
const { readFileSync } = require("fs");

const promptEngine = new PromptEngine();

const yamlConfig = readFileSync("./examples/yaml-examples/general.yaml", "utf8");
promptEngine.loadYAML(yamlConfig);

console.log(promptEngine.buildContext("", true))

/*
Output of this example is:
>> What is the possibility of an event happening? !
Human: Roam around Mars
Bot: This will be possible in a couple years
Human: Drive a car
Bot: This is possible after you get a learner drivers license
>> Starting a new conversation !
Human: Drink water
Bot: Uhm...You don't do that 8 times a day?
Human: Walk on air
Bot: For that you'll need a special device
*/
20 changes: 17 additions & 3 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"typescript": "^4.7.3"
},
"dependencies": {
"gpt3-tokenizer": "^1.1.2"
"gpt3-tokenizer": "^1.1.2",
"yaml": "^2.1.1"
}
}
38 changes: 36 additions & 2 deletions src/ChatEngine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { DefaultModelConfig, PromptEngine } from "./PromptEngine";
import { Interaction, IModelConfig, IChatConfig } from "./types";
import { dashesToCamelCase } from "./utils/utils";

export const DefaultChatConfig: IChatConfig = {
userName: "USER",
botName: "BOT",
newLineOperator: "\n",
newlineOperator: "\n",
};

export class ChatEngine extends PromptEngine {
Expand All @@ -26,7 +27,40 @@ export class ChatEngine extends PromptEngine {
outputPostfix: "",
descriptionPrefix: "",
descriptionPostfix: "",
newLineOperator: languageConfig.newLineOperator,
newlineOperator: languageConfig.newlineOperator,
};
}

protected loadConfigYAML(parsedYAML: Record<string, any>) {
if (parsedYAML["type"] == "chat-engine") {
if (parsedYAML.hasOwnProperty("config")){
const configData = parsedYAML["config"]
if (configData.hasOwnProperty("model-config")) {
const modelConfig = configData["model-config"];
const camelCaseModelConfig = {};
for (const key in modelConfig) {
camelCaseModelConfig[dashesToCamelCase(key)] = modelConfig[key];
}
this.modelConfig = { ...this.modelConfig, ...camelCaseModelConfig };
delete configData["model-config"];
}
const camelCaseConfig = {};
for (const key in configData) {
camelCaseConfig[dashesToCamelCase(key)] = configData[key];
}
this.languageConfig = { ...this.languageConfig, ...camelCaseConfig };
this.promptConfig = {
inputPrefix: this.languageConfig.userName + ":",
inputPostfix: "",
outputPrefix: this.languageConfig.botName + ":",
outputPostfix: "",
descriptionPrefix: "",
descriptionPostfix: "",
newlineOperator: this.languageConfig.newlineOperator,
};
}
} else {
throw Error("Invalid yaml file type");
}
}
}
37 changes: 36 additions & 1 deletion src/CodeEngine.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { DefaultModelConfig, PromptEngine } from "./PromptEngine";
import { Interaction, IModelConfig, ICodePromptConfig } from "./types";
import { dashesToCamelCase } from "./utils/utils";

export const JavaScriptConfig: ICodePromptConfig = {
descriptionCommentOperator: "/*/",
descriptionCloseCommentOperator: "/*/",
commentOperator: "/*",
closeCommentOperator: "*/",
newLineOperator: "\n",
newlineOperator: "\n",
};

export class CodeEngine extends PromptEngine {
Expand All @@ -31,4 +32,38 @@ export class CodeEngine extends PromptEngine {
newLineOperator: languageConfig.newLineOperator,
};
}

protected loadConfigYAML(parsedYAML: Record<string, any>) {
if (parsedYAML["type"] == "code-engine") {
if (parsedYAML.hasOwnProperty("config")){
const configData = parsedYAML["config"]
if (configData.hasOwnProperty("model-config")) {
const modelConfig = configData["model-config"];
const camelCaseModelConfig = {};
for (const key in modelConfig) {
camelCaseModelConfig[dashesToCamelCase(key)] = modelConfig[key];
}
this.modelConfig = { ...this.modelConfig, ...camelCaseModelConfig };
delete configData["model-config"];
}
const camelCaseConfig = {};
for (const key in configData) {
camelCaseConfig[dashesToCamelCase(key)] = configData[key];
}
this.languageConfig = { ...this.languageConfig, ...camelCaseConfig };
this.promptConfig = {
inputPrefix: this.languageConfig.commentOperator,
inputPostfix: this.languageConfig.closeCommentOperator,
outputPrefix: "",
outputPostfix: "",
descriptionPrefix: this.languageConfig.commentOperator,
descriptionPostfix: this.languageConfig.closeCommentOperator,
newlineOperator: this.languageConfig.newlineOperator,
};
}
} else {
throw Error("Invalid yaml file type");
}
}

}
Loading

0 comments on commit 17ad35b

Please sign in to comment.