Skip to content

Commit f8a7ede

Browse files
committed
notes
1 parent 5bdff83 commit f8a7ede

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# AI with Node.js
2+
3+
## Materials
4+
5+
- [notes](https://scottmoss.notion.site/AI-App-Node-js-f9a372a138ef4241943b4fbb44bdc970?pvs=4)

functions.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'dotenv/config'
2+
import { openai } from './openai.js'
3+
import math from 'advanced-calculator'
4+
const QUESTION = process.argv[2] || 'hi'
5+
6+
const messages = [
7+
{
8+
role: 'user',
9+
content: QUESTION,
10+
},
11+
]
12+
13+
const functions = {
14+
calculate: async ({ expression }) => {
15+
return math.evaluate(expression)
16+
},
17+
}
18+
19+
const getCompletion = async (messages) => {
20+
const response = await openai.chat.completions.create({
21+
model: 'gpt-3.5-turbo-0613',
22+
messages,
23+
functions: [
24+
{
25+
name: 'calculate',
26+
description: 'Run a math expression',
27+
parameters: {
28+
type: 'object',
29+
properties: {
30+
expression: {
31+
type: 'string',
32+
description:
33+
'Then math expression to evaluate like "2 * 3 + (21 / 2) ^ 2"',
34+
},
35+
},
36+
required: ['expression'],
37+
},
38+
},
39+
],
40+
temperature: 0,
41+
})
42+
43+
return response
44+
}
45+
46+
let response
47+
while (true) {
48+
response = await getCompletion(messages)
49+
50+
if (response.choices[0].finish_reason === 'stop') {
51+
console.log(response.choices[0].message.content)
52+
break
53+
} else if (response.choices[0].finish_reason === 'function_call') {
54+
const fnName = response.choices[0].message.function_call.name
55+
const args = response.choices[0].message.function_call.arguments
56+
57+
const functionToCall = functions[fnName]
58+
const params = JSON.parse(args)
59+
60+
const result = functionToCall(params)
61+
62+
messages.push({
63+
role: 'assistant',
64+
content: null,
65+
function_call: {
66+
name: fnName,
67+
arguments: args,
68+
},
69+
})
70+
71+
messages.push({
72+
role: 'function',
73+
name: fnName,
74+
content: JSON.stringify({ result: result }),
75+
})
76+
}
77+
}

index.js

Whitespace-only changes.

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14+
"advanced-calculator": "^1.1.1",
1415
"dotenv": "^16.3.1",
1516
"langchain": "^0.0.170",
1617
"openai": "^4.12.4",

qa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const loadStore = async () => {
4747

4848
const query = async () => {
4949
const store = await loadStore()
50-
const results = await store.similaritySearch(question, 2)
50+
const results = await store.similaritySearch(question, 1)
5151

5252
const response = await openai.chat.completions.create({
5353
model: 'gpt-3.5-turbo-16k-0613',

0 commit comments

Comments
 (0)