Skip to content

Commit

Permalink
Refactor code formatting and enhance script generation with template …
Browse files Browse the repository at this point in the history
…handling in prompty.ts
  • Loading branch information
pelikhan committed Sep 4, 2024
1 parent af3bde9 commit b30095c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
51 changes: 47 additions & 4 deletions packages/core/src/prompty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ export interface PromptyFrontmatter {
| JSONSchemaObject
>
outputs?: JSONSchemaObject
model?: {
api?: "chat" | "completion"
configuration?:
| { type?: string; name?: string; organization?: string }
| {
type?: string
api_version?: string
azure_deployment: string
azure_endpoint: string
}
parameters?: {
response_format: "json_object"
max_tokens?: number
temperature?: number
top_p?: number
}
}
}

export interface PromptyDocument {
Expand Down Expand Up @@ -89,10 +106,36 @@ export function promptyToGenAIScript(doc: PromptyDocument) {
}
src += messages
.map((m) => {
const text = String(m.content).replace(
/\{\{([^\}]+)\}\}/g,
(m, name) => "${env.vars." + name + "}"
)
const text = String(m.content)

Check failure on line 109 in packages/core/src/prompty.ts

View workflow job for this annotation

GitHub Actions / build

The regular expression used in the replace function is complex and may lead to performance issues. Consider simplifying it or breaking it down into smaller parts.
.replace(
/\{\{([^\}]+)\}\}/g,
(m, name) => "${env.vars." + name + "}"
)
.replace(
/\{%\s*for\s+(\w+)\s+in\s+([a-zA-Z0-9\.]+)\s*%\}((.|\n)+?){%\s+endfor\s+%\}/gi,
(_, item, col, temp: string) => {
const varname = "env.vars." + item
return (
"${env.vars." +
col +
".map(" +
item +
" => \n`" +
temp
.replace(/^\r?\n/, "")
.replace(
/\$\{([a-z0-9.]+)\}/gi,
(_, v: string) =>
v.startsWith(varname)
? "${" +
v.slice("env.vars.".length) +
"}"
: _
) +
"`).join('')}"
)
}
)
return `$\`${text}\``
})
.join("\n")
Expand Down
38 changes: 25 additions & 13 deletions packages/sample/src/chat.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,28 @@ and in a personable manner using markdown, the customers name and even add some
# Documentation
The following documentation should be used in the response. The response should specifically include the product id.
{% for item in documentation %}
catalog: ${env.vars.item.id}
item: ${env.vars.item.title}
content: ${env.vars.item.content}
{% endfor %}
${env.vars.documentation
.map(
(item) =>
`catalog: ${item.id}
item: ${item.title}
content: ${item.content}
`
)
.join("")}
Make sure to reference any documentation used in the response.
# Previous Orders
Use their orders as context to the question they are asking.
{% for item in customer.orders %}
name: ${env.vars.item.name}
description: ${env.vars.item.description}
{% endfor %}
${env.vars.customer.orders
.map(
(item) =>
`name: ${item.name}
description: ${item.description}
`
)
.join("")}
# Customer Context
Expand All @@ -60,7 +68,11 @@ Reference other items purchased specifically by name and description that
would go well with the items found above. Be brief and concise and use appropriate emojis.
{% for item in history %}
${env.vars.item.role}:
${env.vars.item.content}
{% endfor %}`
${env.vars.history
.map(
(item) =>
`${item.role}:
${item.content}
`
)
.join("")}`

0 comments on commit b30095c

Please sign in to comment.