Skip to content

Commit

Permalink
fix(convert): Added function calling to convert (#10 by @jamonholmgren)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamonholmgren authored Nov 11, 2023
1 parent f2920e8 commit b9479e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"dependencies": {
"gluegun": "latest",
"openai": "^4.7.1"
"openai": "^4.14.2"
},
"devDependencies": {
"@semantic-release/git": "^10.0.1",
Expand Down
40 changes: 16 additions & 24 deletions src/commands/experimental/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
* from one thing to another; whether it's a new version or an alternative library
* or whatever.
*
* It needs some TLC; there are some new features in the OpenAI API (specifically,
* function calling) that would make it much more efficient. (See the "upgrade react-native"
* command as an example of using function calling.)
*
* Additionally, this would be much more useful with gpt-4-32k, which I currently do not
* have access to. Bummer.
*
Expand All @@ -21,7 +17,9 @@
*/
import { GluegunCommand } from 'gluegun'
import { openAI } from '../../ai/openai/openai'
import { MessageParam } from '../../types'
import type { ChatCompletionFunction, MessageParam } from '../../types'
import { patch } from '../../ai/openai/functions/patch'
import { callFunction } from '../../utils/callFunction'

// type for recipes
type Recipe = {
Expand Down Expand Up @@ -50,9 +48,6 @@ const command: GluegunCommand = {
return
}

// get lineChunks parameter
const lineChunks = parameters.options.lineChunks ? parameters.options.lineChunks.split(',').map(Number) : undefined

// show a spinner
print.info(`\nConverting ${sourceFile} from ${from} to ${to}\n`)

Expand Down Expand Up @@ -80,6 +75,7 @@ const command: GluegunCommand = {
const recipeExport = require(`../../recipes/${from}-to-${to}`) as { recipe: Recipe }
recipe = recipeExport.recipe
} catch (e) {
spinner.fail()
print.error(`No recipe found for converting from ${from} to ${to}`)
return
}
Expand All @@ -105,7 +101,7 @@ const command: GluegunCommand = {
role: 'system',
},
{
content: `Here is the source file:\n\n\`\`\`\n${sourceFileContents}\n\`\`\``,
content: `Here is the source file (${sourceFile}):\n\n\`\`\`\n${sourceFileContents}\n\`\`\``,
role: 'system',
},
{
Expand All @@ -115,13 +111,16 @@ const command: GluegunCommand = {
]

const openai = await openAI()

const functions: ChatCompletionFunction[] = [patch]

try {
var response = await openai.chat.completions.create({
model: 'gpt-4',
messages,
// max_tokens: 3000,
// temperature: 0,
// functions, // TODO: important
functions,
user: process.env.USER,
})
} catch (e: any) {
Expand All @@ -141,24 +140,17 @@ const command: GluegunCommand = {
spinner.start()

const message = response.choices[0].message
if (!message) {
print.error('Error or no response from OpenAI')
return
}

// TODO: implement function calling!
const revampedCodeMessage = message.content

if (!revampedCodeMessage) {
print.error('No content found in message: ' + JSON.stringify(message))
if (message.content) {
print.error(message.content)
return
}

// strip any line that starts and ends with backticks
const revampedCode = revampedCodeMessage.replace(/^```.*\n/gm, '').replace(/```.*\n$/gm, '')
const functionName = message.function_call?.name || 'unknown'
const functionArgs = JSON.parse(message.function_call?.arguments || '{}')
const aiMessage = message
const fileData = {} as any

// now write the full revamped code back to the source file
await toolbox.filesystem.writeAsync(sourceFile, revampedCode)
await callFunction({ functionName, functionArgs, functions, aiResponse: aiMessage, fileData })

// update spinner
spinner.succeed()
Expand Down
14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5347,10 +5347,10 @@ open@^9.1.0:
is-inside-container "^1.0.0"
is-wsl "^2.2.0"

openai@^4.7.1:
version "4.7.1"
resolved "https://registry.yarnpkg.com/openai/-/openai-4.7.1.tgz#dbc13f5e830365f8e927026acc5cf68255e57909"
integrity sha512-4Um9A4aLGzZxyENyway0zVgi69BOxaqXmjOCKp3PUteOvSn9TeVf6IjkaNY8k/LXYG5l2e7PpacOl2sxsrTc/w==
openai@^4.14.2:
version "4.14.2"
resolved "https://registry.yarnpkg.com/openai/-/openai-4.14.2.tgz#edc09e986a922da22d14b96b547c29f435e724ae"
integrity sha512-JGlm7mMC7J+cyQZnQMOH7daD9cBqqWqLtlBsejElEkgoehPrYfdyxSxIGICz5xk4YimbwI5FlLATSVojLtCKXQ==
dependencies:
"@types/node" "^18.11.18"
"@types/node-fetch" "^2.6.4"
Expand All @@ -5360,6 +5360,7 @@ openai@^4.7.1:
form-data-encoder "1.7.2"
formdata-node "^4.3.2"
node-fetch "^2.6.7"
web-streams-polyfill "^3.2.1"

optionator@^0.9.3:
version "0.9.3"
Expand Down Expand Up @@ -6827,6 +6828,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz#2898486b74f5156095e473efe989dcf185047a38"
integrity sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==

web-streams-polyfill@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
Expand Down

0 comments on commit b9479e9

Please sign in to comment.