File tree Expand file tree Collapse file tree 6 files changed +90
-1
lines changed Expand file tree Collapse file tree 6 files changed +90
-1
lines changed Original file line number Diff line number Diff line change 1+ # AI with Node.js
2+
3+ ## Materials
4+
5+ - [ notes] ( https://scottmoss.notion.site/AI-App-Node-js-f9a372a138ef4241943b4fbb44bdc970?pvs=4 )
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ const loadStore = async () => {
4747
4848const 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' ,
You can’t perform that action at this time.
0 commit comments