Working with GPT
Integration Dasha with OpenAI GPT
Dasha Scripting Language allows you to integrate GPT flow with common dialog flow using #answerWithGPT function
How does it works?
When you want to response to the user with GPT you can call
var result = #answerWithGPT(`Your prompt`, interruptible:true, gptOptions: { model:"openai/gpt-4", openai_apikey: "YOUR APIKEY" }, sayOptions: { interruptDelay: 1.0 });
Where model:
- openai/gpt-4
- openai/gpt-3.5-turbo
- any other OpenAI model using prefix
openai/
openai_apikey
- your OpenAI APIKEY
interruptible
& interruptDelay
- allows user to interrupt answer process if user is speaking for interruptDelay
seconds
the result will contain information about what happened:
result.interrupted
will be true, if user has interrupted the systemresult.functionCalled
will be true, if gpt requested a function call (see docs below)
GPT Functions
Using GPT functions with DSL can give you a powerfull conversation model.
When you writing any function with JavaDoc like
context { output customerName: string? = null; output customerPhone: string? = null; } /** * Store collected customer information such as name and phone number * @param customerName customer name provided * @param customerPhone customer phone provided * @return true */ function storeCustomerInfo(customerName: string?, customerPhone: string?): boolean { set $this.customerName = $this.customerName ?? customerName; set $this.customerPhone = $this.customerPhone ?? customerPhone; return true; }
Our Dialog Engine passes information about a function storeCustomerInfo
and it's description to the OpenAI inside a request.
And this function will be called when user provides any information described in arguments.
Result of function call will be passed to GPT on next call of #answerWithGPT
/** * Get price of fruit in USD * @param fruit name of fruit * @returns price in USD or `No such fruit` if fruit is not found */ function getFruitPrice(fruit: string) : number|string { if (fruit == "apples") { return 6.29; } if (fruit == "oranges") { return 1.49; } return "No such fruit"; }
Examples
Full DSL example
context { input endpoint: string; } /** * Get price of fruit in USD * @param fruit name of fruit * @returns price in USD or `No such fruit` if fruit is not found */ function getFruitPrice(fruit: string) : number|string { if (fruit == "apples") { return 6.29; } if (fruit == "oranges") { return 1.49; } return "No such fruit"; } start node root { do { #connectSafe($endpoint); #sayText("Hi, this is Mary! How can I help you today?"); wait *; } transitions { gpt: goto gpt on true; } } node gpt { do { // We will be here when user says something, or retry is required var a = #answerWithGPT(`Your name is Mary. You are working in fruit seller contact center. You can only tell customer a price of requested fruit. If you have no price for fruit tell: We have no such fruit `, interruptible:true, gptOptions: { model:"openai/gpt-4" }, sayOptions: { interruptDelay: 1.0 }); // Call answerWithGPT one more time for passing result to the GPT if (a.functionCalled) { #log("Called a function, retry"); goto retry; } wait *; } transitions { gpt: goto gpt on true; retry: goto gpt; } } digression @exit { conditions { on true tags: onclosed; } do { exit; } }