Names and Bindings
Names and Bindings
Names
Names are the basis of all programming languages and are associated with variables, parameters, subprograms, and other variables. Each programming language has differences in design and programming issues. Design issues refer to the specific rules of a language that need to be followed to achieve a specific functionality, while programming style issues refer to the practices of naming styles for readability and consistency that are not limited by the language.
Design Issues
Case Sensitivity
LangChain inherits JavaScript's case sensitivity and aims to ensure no bugs and unintended behavior during runtime (Linux Code, 2023).
In the example, the two variables may have the same names, but the capitalization will differ in the functions and be treated differently whenever being called.
Output shows different values depends on the function called.
Special Words
Special words are the actions named in a language to perform tasks. JavaScript special words are classified as reserved words which it cannot be redefined and used as variable names, label names, or function names (GeeksforGeeks, 2024). The reserved words include:
The sample application of using reserved words.
The output shows a syntax error of the special word 'case'.
Length Limit of JavaScript
JavaScript has different length limits for strings, numbers, and arrays.
Strings
JavaScript also has a limit of characters or strings including the whitespaces and punctuations. The maximum length is 253 - 1, which equals 16384TiB storage using a 64-bit floating-point format (IEEE 754 Standard) (Mozilla Corporation’s, 2024). The devices nowadays, can not hold the amount of memory and tend to lower the threshold. V8, used by Node.js, only supports the maximum length of 229 - 24 (~1GiB), equivalent to 536,870,888.
It will show a range error if the strings exceed the limit in Node.js which 229−23 is 536,870,889 .
2. Numbers
The maximum of numbers in JavaScript is 21024 - 2971, or approximately 1.7976931348623157E+308 (Mozilla Corporation’s, 2023).
In Node.js, if adding 1 to the maximum value, it will still display the finite number of 1.7976931348623157E+308.
Thus, to clarify the limit of numbers, we are going to times 2 the maximum values. The output result in Infinity and cannot shows the exact numbers.
3. Arrays
JavaScript sets a limit of an array with a maximum length of less than 232 -1 (4,294,967,295) with the extension of Node.js extension (Mozilla Corporation’s, 2023).
The array that exceed the limit length will show range error.
To add, JavaScript only accepts a nonnegative integer for array.
Form of Names
The form of names in JavaScript needs to follow specific rules. JavaScript can start with a letter, underscore or dollar sign.
It cannot begin with a number, the output will show an error.
In between the characters, - symbol is not allowed and cause syntax error.
The end of the characters can use any letters, digits, and symbols except operation symbols (+, - *, /), @, #, !, (, ), and %.
The sample application of the unaccepted form of names in JavaScript with Node.js extension. It will display error as the output.
Programming Style Issues
Naming Conventions
Camel notation or camelCase is a naming style where the first letter is lowercase and each subsequent word start with an uppercase letters, making it 'camel' look-alike. It makes the code clear and concise for readability than underscores. It is commonly used for naming objects, functions and local variables.
However, the use of underscores_case typically when the naming styles is all in capital letters. It will be used with constant keywords to emphasize separation in identifiers.
Additionally, PascalCase can be used to name constructors or classes. It starts with a capital letter and a subsequent capital letter.
Despite that, all can be used for LangChain, but which cases are more convenient depends on the context and purposes. The naming styles should always be meaningful to developers and describe the intended action (Linux Code, 2024).
Variables
The simple attributes of variables in JavaScript:
Names
We discussed these attributes in detail in the previous part.
Address
The address is a memory location of variables stored for a program. A variable in JavaScript cannot be associated with different addresses within the same execution. The variable allocation for primitive types like numbers and strings will only reassign a new value in the variable but not to a new address. Meanwhile, for reference types like object and array, the variable will hold memory reference and reassign a new variable if a new memory is located.
Multiple variables can point to the same memory address in JavaScript. The variables will then be dependent on each other as the variable is modified.
3. Type
The suitable operations that can take place when defining is the type attribute. JavaScript does not require you to declare the type of a variable. It will assign the suitable type based on the value or variable during runtime. The data types supported in LangChain in Node.js are: String, Number, BigInt, Boolean, Undefined, Null, Symbol, and Object (W3Schools.com, n.d.).
4. Value
JavaScript can store the primitive values directly while the arrays and functions values by the memory references.
The concept of variables will be continued to discuss in the next sections of binding, scoping, and referencing environments.
- Nur Insyirah Iman
The Concept of Binding
Primarily, the concept of binding is associating entities and attributes together. JavaScript uses dynamic type binding. In this section, we will discover a few ways to implement the concept of binding and how it facilitates us in building an LLM application.
This sample code implements the concept of binding in JavaScript. In the Class Person, two attributes named 'name' and 'age' is created and method 'displayInfo' is used to display the attributes of Class Person. We bind the method 'displayInfo' in each instance using 'bind()' to ensure the method called refers to the correct instance. The source code demonstrates the binding of entities (the 'Person' class) and their attributes ('name' and 'age'). (Langchain, n.d.)
In JavaScript, the binding method is specified with 'this'. 'This' is a keyword, referring to an object. We cannot change the value of 'this' as it is not a variable. (Elliott, 2021) (W3Schools.com, n.d.)
Named constant is a variable that is bound to a value ONLY when it is bound to the storage. In these lines of codes, the variable 'openai' and 'keyword' are bound to the value assigned in 'new OpenAI()' and 'gpt'.
Storage bindings are often created during run-time at the run-time stack when execution has reached the code where a declaration is attached. In JavaScript, it typically uses garbage collector as their memory management where it automatically allocates and deallocates memory (GeeksforGeeks, 2024). JavaScript has 3 stages of the memory life cycle. In the first stage, it will allocate memory to all objects created by the user. Next, objects will use the allocated memory according to instructions. Lastly, the unused memories will be released and used for other purposes.
JavaScript has 2 storages;
Stack (stack dynamic)
Stack is used to store static data, where the size of data is known during compilation time. This includes strings, numbers, boolean, null, and undefined. Static memory allocation has a fixed allocation of memory.
Heap (implicit heap)
A heap is utilized to store objects and functions. Unlike a stack, a heap does not have a fixed amount of memory. Instead, it will allocate memory accordingly when the size is determined during run-time.
Garbage Collection
Garbage collection is the automatic process of obtaining memory that is not occupied to ensure that one's application does not run out of memory during the execution process. (GeeksforGeeks, 2024b)
Implementing the LangChain framework in Node.js offers a wider option of tools that leverage the concept of binding. With access to advanced AI algorithms and models to perform LLM tasks such as text tokenization, part-of-speech tagging, and speech-to-text and text-to-speech, developers can build more advanced and functioning NLP applications effortlessly.
Tool Binding
In the provided source code, the 'bind({tools})' method is used to associate the tools defined with the 'chatOpenAI' model. By binding the tools with the model, we can extend the capabilities of the model to include the ability to call the 'get_current_weather' function whenever we need them.
- Anis Syifaa'
Scope
The scope of a variable is defined as the range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that statement. The scope rules of a language determine how a name is associated with a variable. (Sebesta, 2012).
1. Static Scope (Lexical Scope)
Static scope refers to the ability of a function scope to access variables from the parent scope. JavaScript uses static scope to resolve the variable names when a function is created inside another function. It determines the function's parent scope by looking at where the function was created instead of where it was invoked.
In this case, JavaScript uses Scope Chain to find the variables that are accessible in a certain scope. The scope chain works by searching for a referred variable in the current scope then continue on to the parent scopes, until it reaches the global scope and finds the variable. (Awati, 2022).
Sample Code
x in sub2() function refers to global variable x = 10.
The value returned by sub1() function is not dependent on which function that is calling it.
Even sub2() that has its own variable x = 20 called sub1(), sub1() function will always return the value of global variable x.
2. Dynamic Scope
In dynamic scope, a variable that is known as public variable, can be called from outside of the block of code they are defined. It is based on the calling sequence of subprograms, not on their spatial relationship to each other. Thus, the scope can be determined only at run time. (Awati, 2022).
Sample Code
x in sub2() function refers to sub2()'s x = 20.
x in sub2() function overrides the value of global variable x = 10.
The compiler searches the current block of function first, then successively all the calling functions.
3. Block Scope
Block scope is a section of code that is allowed to have its own local variables whose scope is minimized. (Sebesta, 2012). It is created with a { } and use the keywords of let and const. Variables inside a block cannot be accessed from outside of the block. (W3Schools.com, n.d.).
Sample Code
a is a local variable that is declared in the display_scopes() function.
b is a block-level variable that is declared in the if-block.
4. Global Scope
Global scope is the default scope for all variables that are running in script mode. (Grammar and Types - JavaScript | MDN, 2024). In JavaScript, this language allows a program structure that is a sequence of function definitions, in which variable definitions can appear outside of the functions. (Sebesta, 2012).
Sample Code
message is a global variable, which can be accessed from both inside and outside of functions.
message is accessible from any region of the program.
- Sofia Batrisyia
Scope and Lifetime
The lifetime of a variable or function refers to the period during the execution of a program when it exists and can be used. A variable's lifetime begins when it is declared and ends when its execution completes. The storage duration of the identifier determines its lifetime. (TylerMSFT, 2021)
A local variable is visible only within the function or block where they are defined, and function parameters are always local to their respective functions. They are deleted and their lifetime ends when the function finishes execution. A global variable has a global scope which means it can be accessed throughout the entire script. They are deleted when the browser window or tab is closed. However, it will remain if a new page is loaded in the same browser window.
Global Variables
Lifetime: Global variables exist for the entire duration of the program execution. They are created when the script starts and persist until the program terminates.
In this example, myVar is global and its lifetime extends from when the script starts until it ends.
Local Variables
Lifetime: Local variables are stored in the function's stack frame and they are created when the function is called. They are destroyed and removed from memory when the function execution finishes.
In this example, myVar inside checkscope exists only during the function's execution and is destroyed once it ends, freeing its memory.
(Sebesta, 2012) (W3Schools.com, n.d.) (What Is the Lifetime of JavaScript Variables?, n.d.) (GeeksforGeeks, 2024)
Scope and Lifetime in our Sample Application
Source: https://github.com/developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes
In startRecordingProcess(), global variables such as micInstance, micInputStream, audioChunks, isRecording exists throughout the lifetime of the program. They are reinitialised and the previous instance is replaced.
Source: https://github.com/developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes
In convertResponseToAudio(text), global variables is process.env.ELEVEN_LABS_API_KEY accesses the API key from the global environment. It persiststhroughout the execution of the program. Local Variables such as apiKey, voiceID, fileName, audioStream, fileWriteStream exists temporarily only during the function's execution and are destroyed after completion.
- Kueh Pang Teng
Referencing Environment
The referencing environment is the collection of all variables that are visible in the statement.
JavaScript is primarily a static-scoped or lexical scope language which means that the scope of variables is determined by their location in the source code and does not change based on how functions are called. In a static-scoped language, the variables declared in its local scope and the collection of the variables of its ancestor scopes are visible. The referencing environment is needed when the statement is being compiled so that code and data structures can be created to allow references to variables from other scopes during runtime. In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms.
(Sebesta, 2012) (Noorbakhsh, 2024)
Examples
Global variable g can be read and modified by any function that explicitly accesses the global scope.
Referencing environment of sub1:
Local variables: a, b (accessible within sub1() only)
When g is referenced, it looks in the local environment sub1() but there is no g
It looks at the next outer environment (global). It reads the global value(3) but it cannot be modified without explicit declaration.
Referencing environment of sub2:
Local variable: c
global.g = 10 updates the value of the global variable g to 10 after this function runs.
Directly modifies the global reference
Creates a new local reference for c
Referencing environment of sub3:
Outer Function Environment sub3(): c
Inner Function Environment innerSub3(): g (local, shadows any outer g)
g is found in the inner function's environment (different from the global g)
c is not in the inner environment, so it looks outward and finds c in the outer function
Summary from the code above.
Referencing Environment in our Sample Application
Global variables declared at global scope.
handleSilence function
Source: https://github.com/developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes
When the function handleSilence() is invoked, it becomes the active subprogram.
It interacts with the global variables (isRecording, micInstance, audioChunks) which are available in the global scope. For instance, when handleSilence() accesses isRecording, it references the global scope to read and modify its state. It also controls micInstance and reads audioChunks to process the audio data collected during recording.
The local variables (audioFilename, message, responseText, and fileName) are hidden from the global referencing environment. These variables exist during the function's lifetime and cannot be accessed by other functions or the global scope.
Summary of functions and variables available and their respective referencing environment and lifetime.
- Kueh Pang Teng
REFERENCES
Names and Variables
Linux Code. (2024, December 11). JavaScript Naming Conventions: Best Practices. TheLinuxCode. https://thelinuxcode.com/javascript-naming-conventions-best-practices/
Sebesta, R. W. (n.d.). Concepts of Programming Languages (10th ed.). Pearson Higher Ed. https://www.sci.brooklyn.cuny.edu/~chuang/books/sebesta.pdf
Airbnb. (n.d.). GitHub - airbnb/javascript: JavaScript Style Guide. GitHub. https://github.com/airbnb/javascript?tab=readme-ov-file
Linux Code. (2023, December 27). Is JavaScript case Sensitive? An In-Depth Guide. TheLinuxCode. https://thelinuxcode.com/javascript-case-sensitive/
GeeksforGeeks. (2024, June 19). JavaScript reserved words. GeeksforGeeks. https://www.geeksforgeeks.org/javascript-reserved-words/
Mozilla Corporation’s. (2024, July 15). String: length - JavaScript | MDN. MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
Mozilla Corporation’s. (2023, October 19). Number.MAX_VALUE - JavaScript | MDN. MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE
Mozilla Corporation’s. (2023, September 12). Array: length - JavaScript | MDN. MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
W3Schools.com. (n.d.). https://www.w3schools.com/js/js_datatypes.asp
Developersdigest. (n.d.). GitHub - developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes: https://www.youtube.com/watch?v=myvkeCrw6Rg. GitHub. https://github.com/developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes
The Concept of Binding
Langchain. (n.d.). How to attach runtime arguments to a Runnable. https://js.langchain.com/v0.2/docs/how_to/binding/
Elliott, E. (2021, December 9). What is `this`? The Inner Workings of JavaScript Objects. Medium. https://medium.com/javascript-scene/what-is-this-the-inner-workings-of-javascript-objects-d397bfa0708a#:~:text=Dynamic%20binding%20is%20the%20process,how%20that%20method%20was%20defined.
Zaheer, R. (2023, September 27). Exploring call, apply, and bind methods in JavaScript. Medium. https://medium.com/@rabailzaheer/exploring-call-apply-and-bind-methods-in-javascript-6023627c7bdc
W3Schools.com. (n.d.). https://www.w3schools.com/js/js_this.asp
GeeksforGeeks. (2024, January 9). Memory Management in JavaScript. GeeksforGeeks. https://www.geeksforgeeks.org/memory-management-in-javascript/
GeeksforGeeks. (2024b, December 12). Garbage collection in JavaScript. GeeksforGeeks. https://www.geeksforgeeks.org/garbage-collection-in-javascript/
Scope
programiz. (n.d). JavaScript Variable Scope (with Examples). https://www.programiz.com/javascript/variable-scope
W3Schools.com. (n.d.). https://www.w3schools.com/js/js_scope.asp
Mozilla Corporation’s. (2024, November 14). Grammar and types - JavaScript | MDN. MDN Web Docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variable_scope
Awati, R. (2022, August 25). lexical scoping (static scoping). WhatIs. https://www.techtarget.com/whatis/definition/lexical-scoping-static-scoping
Sebesta, R. W. (2012). Concepts of Programming Languages (Tenth). Pearson Education, Inc.
Scope and Lifetime
Sebesta, R. W. (2012). Concepts of programming languages. Pearson Higher Ed.
W3Schools.com. (n.d.). https://www.w3schools.com/js/js_scope.asp
tutorialspoint. (n.d.) What is the lifetime of JavaScript variables? (n.d.). https://tutorialspoint.com/what-is-the-lifetime-of-javascript-variables
GeeksforGeeks. (2024, June 26). Javascript scope. GeeksforGeeks. https://www.geeksforgeeks.org/javascript-scope/#lifetime-of-javascript-variables
Developersdigest. (n.d.). GitHub - developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes: https://www.youtube.com/watch?v=myvkeCrw6Rg. GitHub. https://github.com/developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes
TylerMSFT. (2021, August 3). Lifetime. Microsoft Learn. https://learn.microsoft.com/en-us/cpp/c-language/lifetime?view=msvc-170
Referencing Environments
Sebesta, R. W. (2012). Concepts of programming languages. Pearson Higher Ed.
Referencing environments. (n.d.). https://courses.cs.vt.edu/cs3304/Spring00/notes/Chapter-4/tsld030.htm
Noorbakhsh, M. (2024, November 23). Dynamic and Static Scope in JavaScript - MohammadMahdi Noorbakhsh - Medium. Medium. https://medium.com/@mhding/dynamic-and-static-scope-in-javascript-4d644bd113de
Developersdigest. (n.d.). GitHub - developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes: https://www.youtube.com/watch?v=myvkeCrw6Rg. GitHub. https://github.com/developersdigest/Create-Your-Own-Voice-Assistant-with-Node.js-Langchain-Eleven-Labs-in-9-Minutes