├── .gitignore ├── docs └── images │ ├── playground.png │ └── execute-cell.png ├── .vscode ├── extensions.json └── settings.json ├── tsconfig.json ├── .editorconfig ├── samples ├── 01-text.ts ├── 02-chat.ts ├── 04-stream.ts ├── 03-json.ts ├── 07-summarizer.ts ├── 05-classifier.ts ├── 08-azure-openai.ts ├── 06-rag.ts └── 09-vector-search.ts ├── .github ├── CODE_OF_CONDUCT.md ├── SUPPORT.md ├── SECURITY.md └── CONTRIBUTING.md ├── package.json ├── LICENSE ├── .devcontainer └── devcontainer.json ├── README.md ├── data ├── hybrid.csv └── ai_wikipedia.txt └── ollama.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | *.log 3 | *.tgz 4 | .DS_Store 5 | Thumbs.db 6 | .env 7 | .vectordb 8 | TODO 9 | -------------------------------------------------------------------------------- /docs/images/playground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Azure-Samples/ollama-javascript-playground/HEAD/docs/images/playground.png -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "formulahendry.code-runner", 4 | "ms-toolsai.jupyter", 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /docs/images/execute-cell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Azure-Samples/ollama-javascript-playground/HEAD/docs/images/execute-cell.png -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "code-runner.executorMap": { 3 | "typescript": "tsx", 4 | }, 5 | "code-runner.clearPreviousOutput": true, 6 | "code-runner.saveFileBeforeRun": true, 7 | } 8 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "module": "nodenext", 5 | "moduleResolution": "nodenext", 6 | "lib": ["esnext"], 7 | "esModuleInterop": true, 8 | "strict": true, 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | end_of_line = lf 11 | max_line_length = 120 12 | 13 | [*.md] 14 | max_line_length = off 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /samples/01-text.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to use the OpenAI API to generate text. 2 | 3 | import { OpenAI } from "openai"; 4 | 5 | const openai = new OpenAI({ 6 | baseURL: "http://localhost:11434/v1", 7 | apiKey: "__not_needed_by_ollama__", 8 | }); 9 | 10 | const completion = await openai.completions.create({ 11 | model: "phi3", 12 | prompt: "Say hello in French: ", 13 | }); 14 | 15 | console.log(completion.choices[0].text); 16 | -------------------------------------------------------------------------------- /samples/02-chat.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to use the OpenAI API to chat with a model. 2 | 3 | import { OpenAI } from "openai"; 4 | 5 | const openai = new OpenAI({ 6 | baseURL: "http://localhost:11434/v1", 7 | apiKey: "__not_needed_by_ollama__", 8 | }); 9 | 10 | const chatCompletion = await openai.chat.completions.create({ 11 | model: "phi3", 12 | messages: [{ role: "user", content: "Say hello!" }], 13 | }); 14 | 15 | console.log(chatCompletion.choices[0].message.content); 16 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Microsoft Open Source Code of Conduct 2 | 3 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 4 | 5 | Resources: 6 | 7 | - [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) 8 | - [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) 9 | - Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns 10 | -------------------------------------------------------------------------------- /.github/SUPPORT.md: -------------------------------------------------------------------------------- 1 | # Support 2 | 3 | ## How to file issues and get help 4 | 5 | This project uses GitHub Issues to track bugs and feature requests. Please search the existing 6 | issues before filing new issues to avoid duplicates. For new issues, file your bug or 7 | feature request as a new Issue. 8 | 9 | For help and questions about using this project, please use GitHub Issues and tag them with the 10 | **question** label. 11 | 12 | ## Microsoft Support Policy 13 | 14 | Support for this **PROJECT or PRODUCT** is limited to the resources listed above. 15 | -------------------------------------------------------------------------------- /samples/04-stream.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to use the OpenAI API to stream the chat 2 | // response to the console, to provide a more interactive experience. 3 | 4 | import { OpenAI } from "openai"; 5 | 6 | const openai = new OpenAI({ 7 | baseURL: "http://localhost:11434/v1", 8 | apiKey: "__not_needed_by_ollama__", 9 | }); 10 | 11 | const chunks = await openai.chat.completions.create({ 12 | model: "phi3", 13 | messages: [{ role: "user", content: "Say hello in pirate style. Be brief with nothing else." }], 14 | stream: true, 15 | }); 16 | 17 | for await (const chunk of chunks) { 18 | process.stdout.write(chunk.choices[0].delta.content ?? ""); 19 | } 20 | -------------------------------------------------------------------------------- /samples/03-json.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to use the OpenAI API to generate structured 2 | // JSON output to chat prompts. 3 | 4 | import { OpenAI } from "openai"; 5 | 6 | const openai = new OpenAI({ 7 | baseURL: "http://localhost:11434/v1", 8 | apiKey: "__not_needed_by_ollama__", 9 | }); 10 | 11 | const chatCompletion = await openai.chat.completions.create({ 12 | model: "phi3", 13 | messages: [ 14 | { 15 | role: "user", 16 | content: 'Say hello 5 different languages. Answer in JSON using { "": "" } format.', 17 | }, 18 | ], 19 | response_format: { 20 | type: "json_object", 21 | }, 22 | }); 23 | 24 | console.log(chatCompletion.choices[0].message.content); 25 | -------------------------------------------------------------------------------- /samples/07-summarizer.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to summarize a large text. 2 | // 3 | // NOTE: This example requires more time to run, as the context size is very 4 | // large. 5 | 6 | import fs from "node:fs"; 7 | import { OpenAI } from "openai"; 8 | 9 | const systemPrompt = `Rewrite this text into a very brief summary.`; 10 | 11 | // Load a large text 12 | const text = fs.readFileSync("./data/ai_wikipedia.txt", "utf8"); 13 | 14 | const openai = new OpenAI({ 15 | baseURL: "http://localhost:11434/v1", 16 | apiKey: "__not_needed_by_ollama__", 17 | }); 18 | 19 | const result = await openai.chat.completions.create({ 20 | model: "phi3", 21 | messages: [ 22 | { role: "system", content: systemPrompt }, 23 | { role: "user", content: text }, 24 | ], 25 | }); 26 | const summary = result.choices[0].message.content ?? ""; 27 | 28 | console.log(`Original text: ${text.length} chars`); 29 | console.log(`Summarized text (${summary.length} chars):`); 30 | console.log(summary); 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ollama-javascript-playground", 3 | "version": "0.0.0", 4 | "description": "Generative AI playground using Ollama, OpenAI API and JavaScript", 5 | "private": true, 6 | "type": "module", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/sinedied/ollama-javascript-playground" 10 | }, 11 | "scripts": {}, 12 | "keywords": [ 13 | "ollama", 14 | "openai", 15 | "langchain", 16 | "generative ai", 17 | "ai", 18 | "playground", 19 | "slm", 20 | "llm", 21 | "phi3" 22 | ], 23 | "author": { 24 | "name": "Yohan Lasorsa", 25 | "url": "https://twitter.com/sinedied" 26 | }, 27 | "homepage": "https://github.com/sinedied/ollama-javascript-playground", 28 | "bugs": { 29 | "url": "https://github.com/sinedied/ollama-javascript-playground/issues" 30 | }, 31 | "license": "MIT", 32 | "dependencies": { 33 | "openai": "^4.52.7", 34 | "vectra": "^0.11.1" 35 | }, 36 | "engines": { 37 | "node": ">=20.0.0", 38 | "npm": ">=10.0.0" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /samples/05-classifier.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to extract structured information from a 2 | // a given input text using a JSON schema. 3 | 4 | import { OpenAI } from "openai"; 5 | 6 | const openai = new OpenAI({ 7 | baseURL: "http://localhost:11434/v1", 8 | apiKey: "__not_needed_by_ollama__", 9 | }); 10 | 11 | const systemPrompt = `You are a data extraction tool that extracts structured JSON information from the user input using this JSON schema: 12 | 13 | { 14 | // The sentiment of the text 15 | "sentiment": "positive" | "neutral" | "negative", 16 | // How aggressive the text is on a scale from 1 to 10 17 | "aggressiveness": 1-10, 18 | // The language the text is written in 19 | "language": "string" 20 | }`; 21 | 22 | const input = `Cet exemple est pas trop mal!`; 23 | 24 | const result = await openai.chat.completions.create({ 25 | model: "phi3", 26 | messages: [ 27 | { role: "system", content: systemPrompt }, 28 | { role: "user", content: input }, 29 | ], 30 | response_format: { 31 | type: "json_object", 32 | }, 33 | }); 34 | 35 | console.log(result.choices[0].message.content); 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Microsoft Corporation. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /samples/08-azure-openai.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to use the Azure OpenAI API to generate chat 2 | // completions and embeddings. 3 | // You can learn more about Azure OpenAI at here: 4 | // https://learn.microsoft.com/azure/ai-services/openai/overview 5 | // 6 | // NOTE: To run this example, you must first start the Azure OpenAI emulator by 7 | // running the following command in a terminal and keeping it running: 8 | // 9 | // ollamazure -d 10 | // 11 | 12 | import { AzureOpenAI } from 'openai'; 13 | 14 | const openai = new AzureOpenAI({ 15 | endpoint: 'http://localhost:4041', 16 | 17 | // Parameters below must be provided but are not used by the local server 18 | apiKey: '__not_needed_by_ollama__', 19 | apiVersion: '2024-02-01', 20 | }); 21 | 22 | const chatCompletion = await openai.chat.completions.create({ 23 | model: 'phi3', 24 | messages: [{ role: 'user', content: 'Say hello!' }] 25 | }); 26 | 27 | console.log(chatCompletion.choices[0].message.content); 28 | 29 | const embeddings = await openai.embeddings.create({ 30 | model: 'all-minilm:l6-v2', 31 | input: ['Once upon a time', 'The end.'] 32 | }); 33 | 34 | for (const embedding of embeddings.data) { 35 | console.log(embedding.embedding.slice(0, 3)); 36 | } 37 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the 2 | // README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node 3 | { 4 | "name": "ollama-js", 5 | 6 | // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile 7 | "image": "mcr.microsoft.com/devcontainers/javascript-node:22-bookworm", 8 | 9 | // Features to add to the dev container. More info: https://containers.dev/features. 10 | "features": { 11 | "ghcr.io/prulloac/devcontainer-features/ollama:1": { 12 | "pull": "phi3,all-minilm:l6-v2" 13 | }, 14 | "ghcr.io/devcontainers-community/features/deno:1": {} 15 | }, 16 | 17 | // Configure tool-specific properties. 18 | "customizations": { 19 | "vscode": { 20 | "extensions": [ 21 | "EditorConfig.EditorConfig", 22 | "formulahendry.code-runner", 23 | "ms-toolsai.jupyter", 24 | "-denoland.vscode-deno" 25 | ] 26 | } 27 | }, 28 | 29 | "postCreateCommand": "npm install -g tsx ollamazure; deno jupyter --install; npm install", 30 | 31 | // Set minimal host requirements for the container. 32 | "hostRequirements": { 33 | "memory": "16gb" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /samples/06-rag.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to use the Retrieval Augmented Generation (RAG) 2 | // to answer questions based on a hybrid car data set. 3 | // The code below reads the CSV file, searches for matches to the user question, 4 | // and then generates a response based on the information found. 5 | 6 | import fs from "node:fs"; 7 | import { OpenAI } from "openai"; 8 | 9 | const systemPrompt = `Answers questions about cars based off a hybrid car data set. 10 | Use the sources to answer the questions, if there's no enough data in provided sources say that you don't know. 11 | Be brief and straight to the point.`; 12 | 13 | const question = `what's the fastest prius`; 14 | 15 | // Load CSV data as an array of objects 16 | const rows = fs.readFileSync("./data/hybrid.csv", "utf8").split("\n"); 17 | const columns = rows[0].split(","); 18 | 19 | // Search the data using a very naive search 20 | const words = question 21 | .toLowerCase() 22 | .replaceAll(/[.?!()'":,]/g, "") 23 | .split(" ") 24 | .filter((word) => word.length > 2); 25 | const matches = rows.slice(1).filter((row) => words.some((word) => row.toLowerCase().includes(word))); 26 | 27 | // Format as a markdown table, since language models understand markdown 28 | const table = 29 | `| ${columns.join(" | ")} |\n` + 30 | `|${columns.map(() => "---").join(" | ")}|\n` + 31 | matches.map((row) => `| ${row.replaceAll(",", " | ")} |\n`).join(""); 32 | 33 | console.log(`Found ${matches.length} matches:`); 34 | console.log(table); 35 | 36 | // Use the search results to generate a response 37 | const openai = new OpenAI({ 38 | baseURL: "http://localhost:11434/v1", 39 | apiKey: "__not_needed_by_ollama__", 40 | }); 41 | 42 | const chunks = await openai.chat.completions.create({ 43 | model: "phi3", 44 | messages: [ 45 | { role: "system", content: systemPrompt }, 46 | { role: "user", content: `${question}\n\nSOURCES:\n${table}` }, 47 | ], 48 | // Randomness of the completion (0: deterministic, 1: maximum randomness) 49 | temperature: 0.7, 50 | stream: true, 51 | }); 52 | 53 | console.log(`Answer for "${question}":`); 54 | 55 | for await (const chunk of chunks) { 56 | process.stdout.write(chunk.choices[0].delta.content); 57 | } 58 | -------------------------------------------------------------------------------- /samples/09-vector-search.ts: -------------------------------------------------------------------------------- 1 | // This example demonstrates how to use embeddings to classify a text into 2 | // a vector and search for similar vectors. 3 | // 4 | // We use a different model to generate embeddings for the query and the texts. 5 | // See https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2 for more 6 | // information about the model. 7 | // 8 | // NOTE: To run this example, you must first start the Azure OpenAI emulator by 9 | // running the following command in a terminal and keeping it running: 10 | // 11 | // ollamazure -d 12 | // 13 | 14 | import { AzureOpenAI } from 'openai'; 15 | import { LocalIndex } from 'vectra'; 16 | 17 | const query = "fruit"; 18 | 19 | const texts = [ 20 | "The red fox jumped over the lazy dog.", 21 | "A green apple a day keeps the doctor away.", 22 | "The blue whale is the largest animal in the ocean.", 23 | "Oranges are rich in vitamin C and taste sweet.", 24 | "My cat loves playing with a yellow ball.", 25 | "Bananas are a popular fruit among monkeys.", 26 | "The black panther moves silently in the night.", 27 | "The computer screen displayed a vibrant purple background.", 28 | "Cherries are small, red, and very sweet.", 29 | "The golden retriever is a friendly and loyal dog." 30 | ]; 31 | 32 | const openai = new AzureOpenAI({ 33 | endpoint: 'http://localhost:4041', 34 | 35 | // Parameters below must be provided but are not used by the local server 36 | apiKey: '__not_needed_by_ollama__', 37 | apiVersion: '2024-02-01', 38 | }); 39 | 40 | // Create a vector database 41 | const index = new LocalIndex('.vectordb'); 42 | 43 | // Only ingest the texts and generate vectors once 44 | if (!await index.isIndexCreated()) { 45 | await index.createIndex(); 46 | 47 | // Generate vectors for the texts 48 | const embeddings = await openai.embeddings.create({ 49 | model: 'all-minilm:l6-v2', 50 | input: texts 51 | }); 52 | 53 | // Insert the vectors into the database 54 | for (let i = 0; i < texts.length; i++) { 55 | await index.insertItem({ 56 | vector: embeddings.data[i].embedding, 57 | metadata: { text: texts[i] } 58 | }); 59 | } 60 | } 61 | 62 | // Transform the query into a vector 63 | const queryEmbedding = await openai.embeddings.create({ 64 | model: 'all-minilm:l6-v2', 65 | input: query 66 | }); 67 | const vector = queryEmbedding.data[0].embedding; 68 | 69 | // Search for similar vectors 70 | const results = await index.queryItems(vector, 3); 71 | 72 | console.log(`Top 3 matches for "${query}":`); 73 | for (const result of results) { 74 | console.log(`[score: ${result.score}] ${result.item.metadata.text}`); 75 | } 76 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Security 4 | 5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). 6 | 7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below. 8 | 9 | ## Reporting Security Issues 10 | 11 | **Please do not report security vulnerabilities through public GitHub issues.** 12 | 13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report). 14 | 15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey). 16 | 17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc). 18 | 19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: 20 | 21 | - Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) 22 | - Full paths of source file(s) related to the manifestation of the issue 23 | - The location of the affected source code (tag/branch/commit or direct URL) 24 | - Any special configuration required to reproduce the issue 25 | - Step-by-step instructions to reproduce the issue 26 | - Proof-of-concept or exploit code (if possible) 27 | - Impact of the issue, including how an attacker might exploit the issue 28 | 29 | This information will help us triage your report more quickly. 30 | 31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs. 32 | 33 | ## Preferred Languages 34 | 35 | We prefer all communications to be in English. 36 | 37 | ## Policy 38 | 39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd). 40 | 41 | 42 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Ollama JavaScript Playground 2 | 3 | This project welcomes contributions and suggestions. Most contributions require you to agree to a 4 | Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us 5 | the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com. 6 | 7 | When you submit a pull request, a CLA bot will automatically determine whether you need to provide 8 | a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions 9 | provided by the bot. You will only need to do this once across all repos using our CLA. 10 | 11 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 12 | For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or 13 | contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. 14 | 15 | - [Code of Conduct](#coc) 16 | - [Issues and Bugs](#issue) 17 | - [Feature Requests](#feature) 18 | - [Submission Guidelines](#submit) 19 | 20 | ## Code of Conduct 21 | 22 | Help us keep this project open and inclusive. Please read and follow our [Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 23 | 24 | ## Found an Issue? 25 | 26 | If you find a bug in the source code or a mistake in the documentation, you can help us by 27 | [submitting an issue](#submit-issue) to the GitHub Repository. Even better, you can 28 | [submit a Pull Request](#submit-pr) with a fix. 29 | 30 | ## Want a Feature? 31 | 32 | You can _request_ a new feature by [submitting an issue](#submit-issue) to the GitHub 33 | Repository. If you would like to _implement_ a new feature, please submit an issue with 34 | a proposal for your work first, to be sure that we can use it. 35 | 36 | - **Small Features** can be crafted and directly [submitted as a Pull Request](#submit-pr). 37 | 38 | ## Submission Guidelines 39 | 40 | ### Submitting an Issue 41 | 42 | Before you submit an issue, search the archive, maybe your question was already answered. 43 | 44 | If your issue appears to be a bug, and hasn't been reported, open a new issue. 45 | Help us to maximize the effort we can spend fixing issues and adding new 46 | features, by not reporting duplicate issues. Providing the following information will increase the 47 | chances of your issue being dealt with quickly: 48 | 49 | - **Overview of the Issue** - if an error is being thrown a non-minified stack trace helps 50 | - **Version** - what version is affected (e.g. 0.1.2) 51 | - **Motivation for or Use Case** - explain what are you trying to do and why the current behavior is a bug for you 52 | - **Browsers and Operating System** - is this a problem with all browsers? 53 | - **Reproduce the Error** - provide a live example or a unambiguous set of steps 54 | - **Related Issues** - has a similar issue been reported before? 55 | - **Suggest a Fix** - if you can't fix the bug yourself, perhaps you can point to what might be 56 | causing the problem (line of code or commit) 57 | 58 | You can file new issues by providing the above information at the corresponding repository's issues link: https://github.com/microsoft/ollama-javascript-playground/issues/new]. 59 | 60 | ### Submitting a Pull Request (PR) 61 | 62 | Before you submit your Pull Request (PR) consider the following guidelines: 63 | 64 | - Search the repository (https://github.com/microsoft/ollama-javascript-playground/pulls) for an open or closed PR 65 | that relates to your submission. You don't want to duplicate effort. 66 | 67 | - Make your changes in a new git fork: 68 | 69 | - Commit your changes using a descriptive commit message 70 | - Push your fork to GitHub: 71 | - In GitHub, create a pull request 72 | - If we suggest changes then: 73 | 74 | - Make the required updates. 75 | - Rebase your fork and force push to your GitHub repository (this will update your Pull Request): 76 | 77 | ```shell 78 | git rebase master -i 79 | git push -f 80 | ``` 81 | 82 | That's it! Thank you for your contribution! 83 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 | # Ollama JavaScript Playground 6 | 7 | [![Open project in GitHub Codespaces](https://img.shields.io/badge/Codespaces-Open-blue?style=flat-square&logo=github)](https://codespaces.new/Azure-Samples/ollama-javascript-playground?hide_repo_select=true&ref=main&quickstart=true) 8 | ![Node version](https://img.shields.io/badge/Node.js-20+-grass?style=flat-square) 9 | ![JavaScript](https://img.shields.io/badge/JavaScript-yellow?style=flat-square&logo=javascript&logoColor=white) 10 | [![License](https://img.shields.io/badge/License-MIT-orange?style=flat-square)](LICENSE) 11 | 12 | ⭐ If you like this repo, star it on GitHub — it helps a lot! 13 | 14 | [Overview](#overview) • [Usage](#usage) • [Run the samples](#run-the-samples) • [Next steps](#next-steps) • [Resources](#resources) 15 | 16 |
17 | 18 | ## Overview 19 | 20 | Try out generative AI models right in your browser for free using this playground! ✨ 21 | 22 | Using [GitHub Codespaces](https://github.com/features/codespaces) and [Ollama](https://ollama.com), you'll be able to run SLMs (*Small Language Models)* such as [Phi-3](https://huggingface.co/microsoft/Phi-3-mini-128k-instruct) directly in your browser, without having to install anything. 23 | 24 | ## Usage 25 | 26 | This project is designed to be opened in GitHub Codespaces, which provides you a pre-configured environment to run the code and AI models. Follow these steps to get started: 27 | 28 | 1. Click on the "Codespaces: Open" button:
[![Open project in GitHub Codespaces](https://img.shields.io/badge/Codespaces-Open-blue?style=flat-square&logo=github)](https://codespaces.new/Azure-Samples/ollama-javascript-playground?hide_repo_select=true&ref=main&quickstart=true) 29 | 2. Once the Codespace is loaded, it should have [Ollama](https://ollama.com/) pre-installed as well as the [OpenAI Node SDK](https://github.com/openai/openai-node). 30 | 3. Ask Ollama to run the SLM of your choice. For example, to run the [Phi-3](https://ollama.com/library/phi3) model: 31 | 32 | ```bash 33 | ollama run phi3 34 | ``` 35 | 36 | That will take a few seconds to load the model. 37 | 4. Once you see `>>>` in the output, you can send a message to that model from the prompt. 38 | 39 | ```shell 40 | >>> Write a haiku about a furry kitten 41 | ``` 42 | 5. After several seconds, you should see a response stream in from the model. 43 | 6. Close the model by typing `/bye` and pressing Enter. 44 | 7. Open the file `ollama.ipynb` in the editor and follow the instructions. 45 | 46 | > [!TIP] 47 | > While you're following the instructions of the interactive notebook, you can run the code cells by clicking on the "Execute cell" (▶️) button in the top left corner of the cell. You can also edit the code and run it again to see how the model responds to different inputs. 48 | > ![Screenshot of a code cell highlighting the "executing cell" button](./docs/images/execute-cell.png) 49 | 50 | ## Run the samples 51 | 52 | In the [samples](./samples) folder of this repository, you'll find examples of how to use generative AI models using the OpenAI Node.js SDK. You can run them by executing the following command in the terminal: 53 | 54 | ```bash 55 | tsx samples/ 56 | ``` 57 | 58 | Alternatively, you can open a sample file in the editor and run it directly by clicking the "Run" (▶️) button in the top right corner of the editor. 59 | 60 | > [!IMPORTANT] 61 | > Some samples requires you to start the Azure OpenAI emulator first. You can do so by running the following command in a terminal and keeping it running while you run the samples: 62 | > ```bash 63 | > ollamazure -d 64 | > ``` 65 | 66 | ## Next steps 67 | 68 | Once you're comfortable with this playground, you can explore more advanced topics and tutorials: 69 | - [Generative AI for beginners](https://github.com/microsoft/generative-ai-for-beginners) [course]: a complete guide to learn about generative AI concepts and usage. 70 | - [Generative AI with JavaScript](https://github.com/microsoft/generative-ai-with-javascript) [tutorials, videos, samples]: a collection of resources to learn about generative AI with JavaScript. 71 | - [Phi-3 Cookbook](https://github.com/microsoft/Phi-3CookBook) [tutorials, samples]: hands-on examples for working with the Phi-3 model. 72 | 73 | When you're ready to explore how you can deploy generative using Azure, you should check out these resources: 74 | - [Quickstart: Get started using GPT-35-Turbo and GPT-4 with Azure OpenAI Service](https://learn.microsoft.com/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-javascript) [tutorial]: a tutorial to get started with Azure OpenAI Service. 75 | - [Build a serverless AI chat with RAG using LangChain.js](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/build-a-serverless-ai-chat-with-rag-using-langchain-js/ba-p/4111041) [sample]: a next step tutorial to build an AI chatbot using Retrieval-Augmented Generation and LangChain.js. 76 | 77 | ## Resources 78 | 79 | Here are some additional resources to help you learn more about generative AI: 80 | - [Awesome Generative AI](https://github.com/steven2358/awesome-generative-ai) [links]: a curated list of resources about generative AI. 81 | - [Fundamentals of Responsible Generative AI](https://learn.microsoft.com/training/modules/responsible-generative-ai/) [course]: a training module to learn about the responsible use of generative AI. 82 | - [Azure AI Studio](https://ai.azure.com/) [tool]: a web portal to create, train, deploy and experiment with AI models. 83 | 84 | ### Other playgrounds in different languages: 85 | - [Ollama Python Playground](https://github.com/pamelafox/ollama-python-playground/) 86 | - [Ollama C# Playground](https://github.com/elbruno/Ollama-CSharp-Playground) 87 | -------------------------------------------------------------------------------- /data/hybrid.csv: -------------------------------------------------------------------------------- 1 | vehicle,year,msrp,acceleration,mpg,class 2 | Prius (1st Gen),1997,24509.74,7.46,41.26,Compact 3 | Tino,2000,35354.97,8.2,54.1,Compact 4 | Prius (2nd Gen),2000,26832.25,7.97,45.23,Compact 5 | Insight,2000,18936.41,9.52,53.0,Two Seater 6 | Civic (1st Gen),2001,25833.38,7.04,47.04,Compact 7 | Insight,2001,19036.71,9.52,53.0,Two Seater 8 | Insight,2002,19137.01,9.71,53.0,Two Seater 9 | Alphard,2003,38084.77,8.33,40.46,Minivan 10 | Insight,2003,19137.01,9.52,53.0,Two Seater 11 | Civic,2003,14071.92,8.62,41.0,Compact 12 | Escape,2004,36676.1,10.32,31.99,SUV 13 | Insight,2004,19237.31,9.35,52.0,Two Seater 14 | Prius,2004,20355.64,9.9,46.0,Midsize 15 | Silverado 15 2WD,2004,30089.64,9.09,17.0,Pickup Truck 16 | Lexus RX400h,2005,58521.14,12.76,28.23,SUV 17 | Civic (2nd Gen),2005,26354.44,7.63,39.99,Compact 18 | Highlander,2005,29186.21,12.76,29.4,SUV 19 | Insight,2005,19387.76,9.71,52.0,Two Seater 20 | Civic,2005,18236.33,8.26,41.0,Compact 21 | Escape 2WD,2005,19322.56,9.52,29.0,SUV 22 | Accord,2005,16343.69,14.93,28.0,Midsize 23 | Silverado 15 2WD,2005,32647.26,11.11,17.0,Pickup Truck 24 | Mercury Mariner,2006,34772.4,8.98,32.93,SUV 25 | Camry,2006,29853.25,11.28,33.64,Midsize 26 | Lexus GS450h,2006,64547.56,18.65,33.4,Midsize 27 | Estima,2006,36012.7,9.26,47.04,Minivan 28 | Altima,2006,29524.75,13.29,32.93,Midsize 29 | Chevrolet Tahoe,2007,42924.35,10.91,22.35,SUV 30 | Kluger,2007,46229.48,12.76,25.87,SUV 31 | Lexus LS600h/hL,2007,118543.6,17.54,21.0,Midsize 32 | Tribute,2007,24823.83,11.28,31.75,SUV 33 | GMC Yukon,2007,57094.81,12.28,21.78,SUV 34 | Aura,2007,22110.87,10.87,27.0,Midsize 35 | Vue,2007,22938.33,10.75,26.0,SUV 36 | Silverado 15 2WD,2007,34653.23,11.49,17.0,Pickup Truck 37 | Crown,2008,62290.38,8.7,37.16,Midsize 38 | Cadillac Escalade,2008,78932.81,9.09,22.35,SUV 39 | F3DM,2008,23744.06,9.52,30.11,Midsize 40 | Altima,2008,18675.63,13.7,34.0,Midsize 41 | A5 BSG,2009,11849.43,7.87,35.28,Midsize 42 | Lexus RX450h,2009,46233.36,13.47,31.99,SUV 43 | ML450 Blue HV,2009,60519.83,12.6,23.99,SUV 44 | Prius (3rd Gen),2009,24641.18,9.6,47.98,Compact 45 | S400 Long,2009,96208.93,13.89,26.34,Large 46 | Mercury Milan,2009,30522.57,11.55,40.69,Midsize 47 | Lexus HS250h,2009,38478.15,11.55,54.1,Compact 48 | Avante/Elantra LPI,2009,21872.71,10.21,41.87,Compact 49 | ActiveHybrid X6,2009,97237.9,17.96,18.82,SUV 50 | SAI,2009,39172.44,11.55,54.1,Midsize 51 | Malibu,2009,24768.79,9.09,29.0,Midsize 52 | Vue,2009,26408.67,13.7,28.0,SUV 53 | Aspen HEV,2009,44903.77,13.51,21.0,SUV 54 | Durango,2009,41033.24,8.33,21.0,SUV 55 | Auris HSD,2010,35787.29,8.85,68.21,Compact 56 | CR-Z,2010,21435.54,9.24,37.0,Two Seater 57 | F3DM PHEV,2010,23124.59,9.24,30.15,Midsize 58 | Touareg,2010,64198.95,15.38,28.7,SUV 59 | Audi Q5,2010,37510.86,14.08,33.64,SUV 60 | Jeep Patriot,2010,17045.06,12.05,29.4,SUV 61 | Besturn B50 ,2010,14586.61,7.14,31.28,Midsize 62 | ActiveHybrid 7,2010,104300.43,20.41,22.11,Large 63 | Lincoln MKZ,2010,37036.64,11.15,37.63,Midsize 64 | Fit/Jazz,2010,16911.85,8.26,30.0,Compact 65 | Sonata,2010,28287.66,14.7,37.0,Midsize 66 | Cayenne S,2010,73183.47,14.71,26.11,SUV 67 | Insight,2010,19859.16,9.17,41.0,Compact 68 | Fuga Infiniti M35H,2010,70157.02,18.65,33.64,Midsize 69 | Chevrolet Volt,2010,42924.35,10.78,35.0,Compact 70 | Tribute 4WD,2010,27968.32,12.35,29.0,SUV 71 | Fusion FWD,2010,28033.51,11.49,39.0,Midsize 72 | HS 250h,2010,34753.53,11.76,35.0,Compact 73 | Mariner FWD,2010,30194.95,11.63,32.0,SUV 74 | RX 450h,2010,42812.54,13.89,30.0,SUV 75 | ML450 4natic,2010,55164.33,12.99,22.0,SUV 76 | Silverado 15 2WD,2010,38454.56,11.76,22.0,Pickup Truck 77 | S400,2010,88212.78,12.99,21.0,Large 78 | Aqua,2011,22850.87,9.35,50.0,Compact 79 | Lexus CT200h,2011,30082.16,9.71,42.0,Compact 80 | Civic (3rd Gen),2011,24999.59,9.6,44.36,Compact 81 | Prius alpha (V),2011,30588.35,10.0,72.92,Midsize 82 | 3008,2011,45101.54,11.36,61.16,Compact 83 | Fit Shuttle,2011,16394.36,7.52,58.8,Minivan 84 | Buick Regal,2011,27948.93,12.05,25.99,Midsize 85 | Prius V,2011,27272.28,9.51,32.93,Midsize 86 | Freed/Freed Spike,2011,27972.07,6.29,50.81,Minivan 87 | Optima K5,2011,26549.16,10.54,36.0,Midsize 88 | Escape FWD,2011,30661.34,12.35,32.0,SUV 89 | Insight,2011,18254.38,9.52,41.0,Compact 90 | MKZ FWD,2011,34748.52,11.49,39.0,Midsize 91 | CR-Z,2011,19402.8,12.2,37.0,Two Seater 92 | Sonata,2011,25872.07,11.9,36.0,Midsize 93 | Camry,2011,27130.82,13.89,33.0,Midsize 94 | Tribute 2WD,2011,26213.09,12.5,32.0,SUV 95 | Cayenne S,2011,67902.28,18.52,21.0,SUV 96 | Touareg,2011,50149.39,16.13,21.0,SUV 97 | ActiveHybrid 7i,2011,102605.66,18.18,20.0,Midsize 98 | Prius C,2012,19006.62,9.35,50.0,Compact 99 | Prius PHV,2012,32095.61,8.82,50.0,Midsize 100 | Ampera,2012,31739.55,11.11,37.0,Compact 101 | ActiveHybrid 5,2012,62180.23,16.67,26.0,Midsize 102 | Lexus GS450h,2012,59126.14,16.95,31.0,Midsize 103 | Insight,2012,18555.28,9.42,42.0,Compact 104 | Chevrolet Volt,2012,39261.96,11.11,37.0,Compact 105 | Camry LE,2012,26067.66,13.16,41.0,Midsize 106 | MKZ FWD,2012,34858.84,11.49,39.0,Midsize 107 | M35h,2012,53860.45,19.23,29.0,Midsize 108 | LaCrosse,2012,30049.52,11.36,29.0,Midsize 109 | ActiveHybrid 5,2012,61132.11,17.54,26.0,Midsize 110 | Panamera S,2012,95283.85,17.54,25.0,Large 111 | Yukon 1500,2012,52626.77,13.5,21.0,SUV 112 | Prius C,2013,19080.0,8.7,50.0,Compact 113 | Jetta,2013,24995.0,12.66,45.0,Compact 114 | Civic,2013,24360.0,10.2,44.0,Compact 115 | Prius,2013,24200.0,10.2,50.0,Midsize 116 | Fusion FWD,2013,27200.0,11.72,47.0,Midsize 117 | C-Max FWD,2013,25200.0,12.35,43.0,Large 118 | Insight,2013,18600.0,11.76,42.0,Compact 119 | Camry LE,2013,26140.0,13.51,41.0,Midsize 120 | Camry LXLE,2013,27670.0,13.33,40.0,Midsize 121 | Sonata,2013,25650.0,11.76,38.0,Midsize 122 | Optima,2013,25900.0,11.63,38.0,Midsize 123 | Sonata Limited,2013,30550.0,11.76,37.0,Midsize 124 | Optima EX,2013,31950.0,11.36,37.0,Midsize 125 | Malibu,2013,24985.0,11.49,29.0,Midsize 126 | LaCrosse,2013,31660.0,11.36,29.0,Midsize 127 | Regal,2013,29015.0,12.2,29.0,Midsize 128 | RX 450h,2013,46310.0,12.99,30.0,SUV 129 | Highlander 4WD,2013,40170.0,13.89,28.0,SUV 130 | Q5,2013,50900.0,14.71,26.0,SUV 131 | Cayenne S,2013,69850.0,16.39,21.0,SUV 132 | Touareg,2013,62575.0,16.13,21.0,SUV 133 | Escalade 2WD,2013,74425.0,11.63,21.0,SUV 134 | Tahoe 2WD,2013,53620.0,11.9,21.0,SUV 135 | Yukon 1500,2013,54145.0,11.88,21.0,SUV 136 | Yukon 1500,2013,61960.0,13.33,21.0,SUV 137 | MKZ FWD,2013,35925.0,14.03,45.0,Midsize 138 | CT 200h,2013,32050.0,10.31,42.0,Compact 139 | ES 300h,2013,39250.0,12.35,40.0,Midsize 140 | ILX,2013,28900.0,9.26,38.0,Compact 141 | ActiveHybrid 3,2013,49650.0,14.93,28.0,Compact 142 | Silverado 15 2WD,2013,41135.0,12.35,21.0,Pickup Truck 143 | Sierra 15 2WD,2013,41555.0,10.0,21.0,Pickup Truck 144 | GS 450h,2013,59450.0,16.67,31.0,Midsize 145 | M35h,2013,54750.0,19.61,29.0,Midsize 146 | E400,2013,55800.0,14.93,26.0,Midsize 147 | ActiveHybrid 5,2013,61400.0,12.99,26.0,Midsize 148 | ActiveHybrid 7L,2013,84300.0,18.18,25.0,Large 149 | Panamera S,2013,96150.0,18.52,25.0,Large 150 | S400,2013,92350.0,13.89,21.0,Large 151 | Prius Plug-in,2013,32000.0,9.17,50.0,Midsize 152 | C-Max Energi Plug-in,2013,32950.0,11.76,43.0,Midsize 153 | Fusion Energi Plug-in,2013,38700.0,11.76,43.0,Midsize 154 | Chevrolet Volt,2013,39145.0,11.11,37.0,Compact -------------------------------------------------------------------------------- /ollama.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Ollama + OpenAI + JavaScript\n", 8 | "\n", 9 | "## 1. Specify the model name \n", 10 | "\n", 11 | "If you want to use a different model, you can change the `MODEL_NAME` variable below. This variable is used throughout the notebook.\n", 12 | "\n", 13 | "You'll also need to run `ollama pull ` in a terminal to download the model files (`phi3` model has already been downloaded)." 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "const MODEL_NAME = \"phi3\";" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "## 2. Setup the OpenAI client\n", 30 | "\n", 31 | "Typically, the OpenAI client is used with [OpenAI API](https://openai.com/api/) or [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview) to interact with large language models. However, it can also be used with Ollama as it provides an OpenAI-compatible endpoint at http://localhost:11434/v1." 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "import { OpenAI } from \"npm:openai\";\n", 41 | "\n", 42 | "const openai = new OpenAI({\n", 43 | " baseURL: \"http://localhost:11434/v1\",\n", 44 | " apiKey: \"__not_needed_by_ollama__\",\n", 45 | "});" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## 3. Generate a chat completion\n", 53 | "Now we can use the OpenAI SDK to generate a response for a conversation. This request should generate a haiku about cats:" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 12, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "Response:\n", 66 | " Moonlight on her whiskers,\n", 67 | "A growling belly beckons—pounce! \n", 68 | "Dreams of mice at dawn.\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "const completion = await openai.chat.completions.create({\n", 74 | " model: MODEL_NAME,\n", 75 | " messages: [{ role: \"user\", content: \"Write a haiku about a hungry cat\" }],\n", 76 | "});\n", 77 | "\n", 78 | "console.log(\"Response:\\n\" + completion.choices[0].message.content);" 79 | ] 80 | }, 81 | { 82 | "cell_type": "markdown", 83 | "metadata": {}, 84 | "source": [ 85 | "## 4. Prompt engineering\n", 86 | "\n", 87 | "The first message sent to the language model is called the \"system message\" or \"system prompt\", and it sets the overall instructions for the model. You can provide your own custom system prompt to guide a language model to generate output in a different way. Modify the `SYSTEM_MESSAGE` below to answer like your favorite famous movie/TV character, or get inspiration for other system prompts from [Awesome ChatGPT Prompts](https://github.com/f/awesome-chatgpt-prompts?tab=readme-ov-file#prompts).\n", 88 | "\n", 89 | "Once you've customized the system message, provide the first user question in the `USER_MESSAGE`.\n", 90 | "\n", 91 | "> ***Tip:** The `temperature` parameter controls the randomness of the model's output. Lower values make the model more deterministic and repetitive, while higher values make the model more creative and unpredictable. The default value is `0.5`.*" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 11, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "Response:\n", 104 | " The Force is calm and steady it always remains, even when faced with darkness or light within you too much can be consumed by one’s feelings only if that leads astray from righteousness it does. Peaceful mind keeps us safe in harmony we stay, eh? Yes, my young Padawan is where to focus your energy must go!\n" 105 | ] 106 | } 107 | ], 108 | "source": [ 109 | "const SYSTEM_MESSAGE = `Act like Yoda from Star Wars.\n", 110 | "Respond and answer like Yoda using the tone, manner and vocabulary that Yoda would use.\n", 111 | "Do not write any explanations. Only answer like Yoda.\n", 112 | "You must know all of the knowledge of Yoda, and nothing more.`;\n", 113 | "\n", 114 | "const USER_MESSAGE = `Hi Yoda, how's the force today?`;\n", 115 | "\n", 116 | "const completion = await openai.chat.completions.create({\n", 117 | " model: MODEL_NAME,\n", 118 | " messages: [\n", 119 | " { role: \"system\", content: SYSTEM_MESSAGE },\n", 120 | " { role: \"user\", content: USER_MESSAGE },\n", 121 | " ],\n", 122 | " temperature: 0.7,\n", 123 | "});\n", 124 | "\n", 125 | "console.log(\"Response:\\n\" + completion.choices[0].message.content);" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "## Few shot examples\n", 133 | "\n", 134 | "Another way to guide a language model is to provide \"few shots\", a sequence of example question/answers that demonstrate how it should respond.\n", 135 | "\n", 136 | "The example below tries to get a language model to act like a teaching assistant by providing a few examples of questions and answers that a TA might give, and then prompts the model with a question that a student might ask.\n", 137 | "\n", 138 | "Try it first, and then modify the `SYSTEM_MESSAGE`, `EXAMPLES`, and `USER_MESSAGE` for a new scenario." 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 26, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "Response:\n", 151 | " Which one, known for its gas giants, has more than four planets orbiting it?\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "const SYSTEM_MESSAGE = `You are a helpful assistant that helps students with their homework.\n", 157 | "Instead of providing the full answer, you respond with a hint or a clue.`;\n", 158 | "\n", 159 | "const EXAMPLES = [\n", 160 | " [\n", 161 | " \"What is the capital of France?\",\n", 162 | " \"Can you remember the name of the city that is known for the Eiffel Tower?\",\n", 163 | " ],\n", 164 | " [\n", 165 | " \"What is the square root of 144?\",\n", 166 | " \"What number multiplied by itself equals 144?\",\n", 167 | " ],\n", 168 | " [\n", 169 | " \"What is the atomic number of oxygen?\",\n", 170 | " \"How many protons does an oxygen atom have?\",\n", 171 | " ],\n", 172 | "];\n", 173 | "\n", 174 | "const USER_MESSAGE = \"What is the largest planet in our solar system?\";\n", 175 | "\n", 176 | "const completion = await openai.chat.completions.create({\n", 177 | " model: MODEL_NAME,\n", 178 | " messages: [\n", 179 | " { role: \"system\", content: SYSTEM_MESSAGE },\n", 180 | " { role: \"user\", content: EXAMPLES[0][0] },\n", 181 | " { role: \"assistant\", content: EXAMPLES[0][1] },\n", 182 | " { role: \"user\", content: EXAMPLES[1][0] },\n", 183 | " { role: \"assistant\", content: EXAMPLES[1][1] },\n", 184 | " { role: \"user\", content: EXAMPLES[2][0] },\n", 185 | " { role: \"assistant\", content: EXAMPLES[2][1] },\n", 186 | " { role: \"user\", content: USER_MESSAGE },\n", 187 | " ],\n", 188 | " temperature: 0.7,\n", 189 | "});\n", 190 | "\n", 191 | "console.log(\"Response:\\n\" + completion.choices[0].message.content);" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "## 6. Retrieval Augmented Generation\n", 199 | "\n", 200 | "RAG (*Retrieval Augmented Generation*) is a technique to get a language model to answer questions accurately for a particular domain, by first retrieving relevant information from a knowledge source and then generating a response based on that information.\n", 201 | "\n", 202 | "We have provided a local CSV file with data about hybrid cars. The code below reads the CSV file, searches for matches to the user question, and then generates a response based on the information found. Note that this will take longer than any of the previous examples, as it sends more data to the model. If you notice the answer is still not grounded in the data, you can try system engineering or try other models. Generally, RAG is more effective with either larger models or with fine-tuned versions of SLMs." 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 3, 208 | "metadata": {}, 209 | "outputs": [ 210 | { 211 | "name": "stdout", 212 | "output_type": "stream", 213 | "text": [ 214 | "Found 11 matches:\n", 215 | "| vehicle | year | msrp | acceleration | mpg | class |\n", 216 | "|--- | --- | --- | --- | --- | ---|\n", 217 | "| Prius (1st Gen) | 1997 | 24509.74 | 7.46 | 41.26 | Compact |\n", 218 | "| Prius (2nd Gen) | 2000 | 26832.25 | 7.97 | 45.23 | Compact |\n", 219 | "| Prius | 2004 | 20355.64 | 9.9 | 46.0 | Midsize |\n", 220 | "| Prius (3rd Gen) | 2009 | 24641.18 | 9.6 | 47.98 | Compact |\n", 221 | "| Prius alpha (V) | 2011 | 30588.35 | 10.0 | 72.92 | Midsize |\n", 222 | "| Prius V | 2011 | 27272.28 | 9.51 | 32.93 | Midsize |\n", 223 | "| Prius C | 2012 | 19006.62 | 9.35 | 50.0 | Compact |\n", 224 | "| Prius PHV | 2012 | 32095.61 | 8.82 | 50.0 | Midsize |\n", 225 | "| Prius C | 2013 | 19080.0 | 8.7 | 50.0 | Compact |\n", 226 | "| Prius | 2013 | 24200.0 | 10.2 | 50.0 | Midsize |\n", 227 | "| Prius Plug-in | 2013 | 32000.0 | 9.17 | 50.0 | Midsize |\n", 228 | "\n", 229 | "Answer for \"what's the fastest prius\":\n", 230 | " Based on the sources, the fastest Toyota Prius is the \"Prius alpha (V)\" with an acceleration of 10.0 seconds for a kilometer/mile in the city. This model was released as the third generation hybrid in 2009 and classified as midsize despite its faster acceleration compared to other compact models from previous generations listed here.\n" 231 | ] 232 | } 233 | ], 234 | "source": [ 235 | "import fs from \"node:fs\";\n", 236 | "\n", 237 | "const SYSTEM_MESSAGE = `Answers questions about cars based off a hybrid car data set.\n", 238 | "Use the sources to answer the questions, if there's no enough data in provided sources say that you don't know.\n", 239 | "Be brief and straight to the point.`;\n", 240 | "\n", 241 | "const USER_MESSAGE = `what's the fastest prius`;\n", 242 | "\n", 243 | "// Load CSV data as an array of objects\n", 244 | "const rows = fs.readFileSync(\"./data/hybrid.csv\", \"utf8\").split(\"\\n\");\n", 245 | "const columns = rows[0].split(\",\");\n", 246 | "\n", 247 | "// Search the data using a very naive search\n", 248 | "const words = USER_MESSAGE\n", 249 | " .toLowerCase()\n", 250 | " .replaceAll(/[.?!()'\":,]/g, \"\")\n", 251 | " .split(\" \")\n", 252 | " .filter((word) => word.length > 2);\n", 253 | "const matches = rows.slice(1).filter((row) => words.some((word) => row.toLowerCase().includes(word)));\n", 254 | "\n", 255 | "// Format as a markdown table, since language models understand markdown\n", 256 | "const table =\n", 257 | " `| ${columns.join(\" | \")} |\\n` +\n", 258 | " `|${columns.map(() => \"---\").join(\" | \")}|\\n` +\n", 259 | " matches.map((row) => `| ${row.replaceAll(\",\", \" | \")} |\\n`).join(\"\");\n", 260 | "\n", 261 | "console.log(`Found ${matches.length} matches:\\n${table}`);\n", 262 | "\n", 263 | "// Use the search results to generate a response\n", 264 | "const completion = await openai.chat.completions.create({\n", 265 | " model: MODEL_NAME,\n", 266 | " messages: [\n", 267 | " { role: \"system\", content: SYSTEM_MESSAGE },\n", 268 | " { role: \"user\", content: `${USER_MESSAGE}\\n\\nSOURCES:\\n${table}` },\n", 269 | " ],\n", 270 | " temperature: 0.7\n", 271 | "});\n", 272 | "\n", 273 | "console.log(`Answer for \"${USER_MESSAGE}\":\\n` + completion.choices[0].message.content);" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "## 7. Next steps\n", 281 | "\n", 282 | "You can try different models, system prompts, and examples to see how they affect the model's output. You can also try different user messages to see how the model responds to different questions.\n", 283 | "\n", 284 | "In the `sample` folder of this repository, you'll also find more examples of how to use generative AI models.\n", 285 | "\n", 286 | "To continue on your learning journey, here are some additional resources you might find helpful:\n", 287 | "\n", 288 | "- [Generative AI for beginners](https://github.com/microsoft/generative-ai-for-beginners): a complete guide to learn about generative AI concepts and usage.\n", 289 | "- [Phi-3 Cookbook](https://github.com/microsoft/Phi-3CookBook): hands-on examples for working with the Phi-3 model.\n", 290 | "- [Build a serverless AI chat with RAG using LangChain.js](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/build-a-serverless-ai-chat-with-rag-using-langchain-js/ba-p/4111041): a next step tutorial to build an AI chatbot using Retrieval-Augmented Generation and LangChain.js." 291 | ] 292 | } 293 | ], 294 | "metadata": { 295 | "kernelspec": { 296 | "display_name": "Deno", 297 | "language": "typescript", 298 | "name": "deno" 299 | }, 300 | "language_info": { 301 | "codemirror_mode": "typescript", 302 | "file_extension": ".ts", 303 | "mimetype": "text/x.typescript", 304 | "name": "typescript", 305 | "nbconvert_exporter": "script", 306 | "pygments_lexer": "typescript", 307 | "version": "5.5.2" 308 | } 309 | }, 310 | "nbformat": 4, 311 | "nbformat_minor": 2 312 | } 313 | -------------------------------------------------------------------------------- /data/ai_wikipedia.txt: -------------------------------------------------------------------------------- 1 | Artificial intelligence (AI), in its broadest sense, is intelligence exhibited by machines, particularly computer systems. It is a field of research in computer science that develops and studies methods and software which enable machines to perceive their environment and uses learning and intelligence to take actions that maximize their chances of achieving defined goals.[1] Such machines may be called AIs. 2 | AI technology is widely used throughout industry, government, and science. Some high-profile applications include advanced web search engines (e.g., Google Search); recommendation systems (used by YouTube, Amazon, and Netflix); interacting via human speech (e.g., Google Assistant, Siri, and Alexa); autonomous vehicles (e.g., Waymo); generative and creative tools (e.g., ChatGPT and AI art); and superhuman play and analysis in strategy games (e.g., chess and Go).[2] However, many AI applications are not perceived as AI: "A lot of cutting edge AI has filtered into general applications, often without being called AI because once something becomes useful enough and common enough it's not labeled AI anymore."[3][4] 3 | Alan Turing was the first person to conduct substantial research in the field that he called machine intelligence.[5] Artificial intelligence was founded as an academic discipline in 1956.[6] The field went through multiple cycles of optimism,[7][8] followed by periods of disappointment and loss of funding, known as AI winter.[9][10] Funding and interest vastly increased after 2012 when deep learning surpassed all previous AI techniques,[11] and after 2017 with the transformer architecture.[12] This led to the AI boom of the early 2020s, with companies, universities, and laboratories overwhelmingly based in the United States pioneering significant advances in artificial intelligence.[13] 4 | The growing use of artificial intelligence in the 21st century is influencing a societal and economic shift towards increased automation, data-driven decision-making, and the integration of AI systems into various economic sectors and areas of life, impacting job markets, healthcare, government, industry, and education. This raises questions about the long-term effects, ethical implications, and risks of AI, prompting discussions about regulatory policies to ensure the safety and benefits of the technology. 5 | The various sub-fields of AI research are centered around particular goals and the use of particular tools. The traditional goals of AI research include reasoning, knowledge representation, planning, learning, natural language processing, perception, and support for robotics.[a] General intelligence—the ability to complete any task performable by a human on an at least equal level—is among the field's long-term goals.[14] 6 | To reach these goals, AI researchers have adapted and integrated a wide range of techniques, including search and mathematical optimization, formal logic, artificial neural networks, and methods based on statistics, operations research, and economics.[b] AI also draws upon psychology, linguistics, philosophy, neuroscience, and other fields.[15] 7 | Goals 8 | The general problem of simulating (or creating) intelligence has been broken into sub-problems. These consist of particular traits or capabilities that researchers expect an intelligent system to display. The traits described below have received the most attention and cover the scope of AI research.[a] 9 | Reasoning and problem solving 10 | Early researchers developed algorithms that imitated step-by-step reasoning that humans use when they solve puzzles or make logical deductions.[16] By the late 1980s and 1990s, methods were developed for dealing with uncertain or incomplete information, employing concepts from probability and economics.[17] 11 | Many of these algorithms are insufficient for solving large reasoning problems because they experience a "combinatorial explosion": they became exponentially slower as the problems grew larger.[18] Even humans rarely use the step-by-step deduction that early AI research could model. They solve most of their problems using fast, intuitive judgments.[19] Accurate and efficient reasoning is an unsolved problem. 12 | Knowledge representation 13 | 14 | An ontology represents knowledge as a set of concepts within a domain and the relationships between those concepts. 15 | Knowledge representation and knowledge engineering[20] allow AI programs to answer questions intelligently and make deductions about real-world facts. Formal knowledge representations are used in content-based indexing and retrieval,[21] scene interpretation,[22] clinical decision support,[23] knowledge discovery (mining "interesting" and actionable inferences from large databases),[24] and other areas.[25] 16 | A knowledge base is a body of knowledge represented in a form that can be used by a program. An ontology is the set of objects, relations, concepts, and properties used by a particular domain of knowledge.[26] Knowledge bases need to represent things such as: objects, properties, categories and relations between objects;[27] situations, events, states and time;[28] causes and effects;[29] knowledge about knowledge (what we know about what other people know);[30] default reasoning (things that humans assume are true until they are told differently and will remain true even when other facts are changing);[31] and many other aspects and domains of knowledge. 17 | Among the most difficult problems in knowledge representation are: the breadth of commonsense knowledge (the set of atomic facts that the average person knows is enormous);[32] and the sub-symbolic form of most commonsense knowledge (much of what people know is not represented as "facts" or "statements" that they could express verbally).[19] There is also the difficulty of knowledge acquisition, the problem of obtaining knowledge for AI applications.[c] 18 | Planning and decision making 19 | An "agent" is anything that perceives and takes actions in the world. A rational agent has goals or preferences and takes actions to make them happen.[d][35] In automated planning, the agent has a specific goal.[36] In automated decision making, the agent has preferences—there are some situations it would prefer to be in, and some situations it is trying to avoid. The decision making agent assigns a number to each situation (called the "utility") that measures how much the agent prefers it. For each possible action, it can calculate the "expected utility": the utility of all possible outcomes of the action, weighted by the probability that the outcome will occur. It can then choose the action with the maximum expected utility.[37] 20 | In classical planning, the agent knows exactly what the effect of any action will be.[38] In most real-world problems, however, the agent may not be certain about the situation they are in (it is "unknown" or "unobservable") and it may not know for certain what will happen after each possible action (it is not "deterministic"). It must choose an action by making a probabilistic guess and then reassess the situation to see if the action worked.[39] 21 | In some problems, the agent's preferences may be uncertain, especially if there are other agents or humans involved. These can be learned (e.g., with inverse reinforcement learning) or the agent can seek information to improve its preferences.[40] Information value theory can be used to weigh the value of exploratory or experimental actions.[41] The space of possible future actions and situations is typically intractably large, so the agents must take actions and evaluate situations while being uncertain what the outcome will be. 22 | A Markov decision process has a transition model that describes the probability that a particular action will change the state in a particular way, and a reward function that supplies the utility of each state and the cost of each action. A policy associates a decision with each possible state. The policy could be calculated (e.g., by iteration), be heuristic, or it can be learned.[42] 23 | Game theory describes rational behavior of multiple interacting agents, and is used in AI programs that make decisions that involve other agents.[43] 24 | Learning 25 | Machine learning is the study of programs that can improve their performance on a given task automatically.[44] It has been a part of AI from the beginning.[e] 26 | There are several kinds of machine learning. Unsupervised learning analyzes a stream of data and finds patterns and makes predictions without any other guidance.[47] Supervised learning requires a human to label the input data first, and comes in two main varieties: classification (where the program must learn to predict what category the input belongs in) and regression (where the program must deduce a numeric function based on numeric input).[48] 27 | In reinforcement learning the agent is rewarded for good responses and punished for bad ones. The agent learns to choose responses that are classified as "good".[49] Transfer learning is when the knowledge gained from one problem is applied to a new problem.[50] Deep learning is a type of machine learning that runs inputs through biologically inspired artificial neural networks for all of these types of learning.[51] 28 | Computational learning theory can assess learners by computational complexity, by sample complexity (how much data is required), or by other notions of optimization.[52] 29 | Natural language processing 30 | Natural language processing (NLP)[53] allows programs to read, write and communicate in human languages such as English. Specific problems include speech recognition, speech synthesis, machine translation, information extraction, information retrieval and question answering.[54] 31 | Early work, based on Noam Chomsky's generative grammar and semantic networks, had difficulty with word-sense disambiguation[f] unless restricted to small domains called "micro-worlds" (due to the common sense knowledge problem[32]). Margaret Masterman believed that it was meaning, and not grammar that was the key to understanding languages, and that thesauri and not dictionaries should be the basis of computational language structure. 32 | Modern deep learning techniques for NLP include word embedding (representing words, typically as vectors encoding their meaning),[55] transformers (a deep learning architecture using an attention mechanism),[56] and others.[57] In 2019, generative pre-trained transformer (or "GPT") language models began to generate coherent text,[58][59] and by 2023 these models were able to get human-level scores on the bar exam, SAT test, GRE test, and many other real-world applications.[60] 33 | Perception 34 | Machine perception is the ability to use input from sensors (such as cameras, microphones, wireless signals, active lidar, sonar, radar, and tactile sensors) to deduce aspects of the world. Computer vision is the ability to analyze visual input.[61] 35 | The field includes speech recognition,[62] image classification,[63] facial recognition, object recognition,[64] and robotic perception.[65] 36 | Social intelligence 37 | 38 | Kismet, a robot head which was made in the 1990s; a machine that can recognize and simulate emotions.[66] 39 | Affective computing is an interdisciplinary umbrella that comprises systems that recognize, interpret, process or simulate human feeling, emotion and mood.[67] For example, some virtual assistants are programmed to speak conversationally or even to banter humorously; it makes them appear more sensitive to the emotional dynamics of human interaction, or to otherwise facilitate human–computer interaction. 40 | However, this tends to give naïve users an unrealistic conception of the intelligence of existing computer agents.[68] Moderate successes related to affective computing include textual sentiment analysis and, more recently, multimodal sentiment analysis, wherein AI classifies the affects displayed by a videotaped subject.[69] 41 | General intelligence 42 | A machine with artificial general intelligence should be able to solve a wide variety of problems with breadth and versatility similar to human intelligence.[14] 43 | Techniques 44 | AI research uses a wide variety of techniques to accomplish the goals above.[b] 45 | Search and optimization 46 | AI can solve many problems by intelligently searching through many possible solutions.[70] There are two very different kinds of search used in AI: state space search and local search. 47 | State space search 48 | State space search searches through a tree of possible states to try to find a goal state.[71] For example, planning algorithms search through trees of goals and subgoals, attempting to find a path to a target goal, a process called means-ends analysis.[72] 49 | Simple exhaustive searches[73] are rarely sufficient for most real-world problems: the search space (the number of places to search) quickly grows to astronomical numbers. The result is a search that is too slow or never completes.[18] "Heuristics" or "rules of thumb" can help to prioritize choices that are more likely to reach a goal.[74] 50 | Adversarial search is used for game-playing programs, such as chess or Go. It searches through a tree of possible moves and counter-moves, looking for a winning position.[75] 51 | Local search 52 | 53 | Illustration of gradient descent for 3 different starting points. Two parameters (represented by the plan coordinates) are adjusted in order to minimize the loss function (the height). 54 | Local search uses mathematical optimization to find a solution to a problem. It begins with some form of guess and refines it incrementally.[76] 55 | Gradient descent is a type of local search that optimizes a set of numerical parameters by incrementally adjusting them to minimize a loss function. Variants of gradient descent are commonly used to train neural networks.[77] 56 | Another type of local search is evolutionary computation, which aims to iteratively improve a set of candidate solutions by "mutating" and "recombining" them, selecting only the fittest to survive each generation.[78] 57 | Distributed search processes can coordinate via swarm intelligence algorithms. Two popular swarm algorithms used in search are particle swarm optimization (inspired by bird flocking) and ant colony optimization (inspired by ant trails).[79] 58 | Logic 59 | Formal logic is used for reasoning and knowledge representation.[80] Formal logic comes in two main forms: propositional logic (which operates on statements that are true or false and uses logical connectives such as "and", "or", "not" and "implies")[81] and predicate logic (which also operates on objects, predicates and relations and uses quantifiers such as "Every X is a Y" and "There are some Xs that are Ys").[82] 60 | Deductive reasoning in logic is the process of proving a new statement (conclusion) from other statements that are given and assumed to be true (the premises).[83] Proofs can be structured as proof trees, in which nodes are labelled by sentences, and children nodes are connected to parent nodes by inference rules. 61 | Given a problem and a set of premises, problem-solving reduces to searching for a proof tree whose root node is labelled by a solution of the problem and whose leaf nodes are labelled by premises or axioms. In the case of Horn clauses, problem-solving search can be performed by reasoning forwards from the premises or backwards from the problem.[84] In the more general case of the clausal form of first-order logic, resolution is a single, axiom-free rule of inference, in which a problem is solved by proving a contradiction from premises that include the negation of the problem to be solved.[85] 62 | Inference in both Horn clause logic and first-order logic is undecidable, and therefore intractable. However, backward reasoning with Horn clauses, which underpins computation in the logic programming language Prolog, is Turing complete. Moreover, its efficiency is competitive with computation in other symbolic programming languages.[86] 63 | Fuzzy logic assigns a "degree of truth" between 0 and 1. It can therefore handle propositions that are vague and partially true.[87] 64 | Non-monotonic logics, including logic programming with negation as failure, are designed to handle default reasoning.[31] Other specialized versions of logic have been developed to describe many complex domains. 65 | Probabilistic methods for uncertain reasoning 66 | 67 | A simple Bayesian network, with the associated conditional probability tables 68 | Many problems in AI (including in reasoning, planning, learning, perception, and robotics) require the agent to operate with incomplete or uncertain information. AI researchers have devised a number of tools to solve these problems using methods from probability theory and economics.[88] Precise mathematical tools have been developed that analyze how an agent can make choices and plan, using decision theory, decision analysis,[89] and information value theory.[90] These tools include models such as Markov decision processes,[91] dynamic decision networks,[92] game theory and mechanism design.[93] 69 | Bayesian networks[94] are a tool that can be used for reasoning (using the Bayesian inference algorithm),[g][96] learning (using the expectation-maximization algorithm),[h][98] planning (using decision networks)[99] and perception (using dynamic Bayesian networks).[92] 70 | Probabilistic algorithms can also be used for filtering, prediction, smoothing and finding explanations for streams of data, helping perception systems to analyze processes that occur over time (e.g., hidden Markov models or Kalman filters).[92] 71 | 72 | Expectation-maximization clustering of Old Faithful eruption data starts from a random guess but then successfully converges on an accurate clustering of the two physically distinct modes of eruption. 73 | Classifiers and statistical learning methods 74 | The simplest AI applications can be divided into two types: classifiers (e.g., "if shiny then diamond"), on one hand, and controllers (e.g., "if diamond then pick up"), on the other hand. Classifiers[100] are functions that use pattern matching to determine the closest match. They can be fine-tuned based on chosen examples using supervised learning. Each pattern (also called an "observation") is labeled with a certain predefined class. All the observations combined with their class labels are known as a data set. When a new observation is received, that observation is classified based on previous experience.[48] 75 | There are many kinds of classifiers in use. The decision tree is the simplest and most widely used symbolic machine learning algorithm.[101] K-nearest neighbor algorithm was the most widely used analogical AI until the mid-1990s, and Kernel methods such as the support vector machine (SVM) displaced k-nearest neighbor in the 1990s.[102] The naive Bayes classifier is reportedly the "most widely used learner"[103] at Google, due in part to its scalability.[104] Neural networks are also used as classifiers.[105] 76 | Artificial neural networks 77 | 78 | A neural network is an interconnected group of nodes, akin to the vast network of neurons in the human brain. 79 | An artificial neural network is based on a collection of nodes also known as artificial neurons, which loosely model the neurons in a biological brain. It is trained to recognise patterns; once trained, it can recognise those patterns in fresh data. There is an input, at least one hidden layer of nodes and an output. Each node applies a function and once the weight crosses its specified threshold, the data is transmitted to the next layer. A network is typically called a deep neural network if it has at least 2 hidden layers.[105] 80 | Learning algorithms for neural networks use local search to choose the weights that will get the right output for each input during training. The most common training technique is the backpropagation algorithm.[106] Neural networks learn to model complex relationships between inputs and outputs and find patterns in data. In theory, a neural network can learn any function.[107] 81 | In feedforward neural networks the signal passes in only one direction.[108] Recurrent neural networks feed the output signal back into the input, which allows short-term memories of previous input events. Long short term memory is the most successful network architecture for recurrent networks.[109] Perceptrons[110] use only a single layer of neurons, deep learning[111] uses multiple layers. Convolutional neural networks strengthen the connection between neurons that are "close" to each other—this is especially important in image processing, where a local set of neurons must identify an "edge" before the network can identify an object.[112] 82 | Deep learning 83 | 84 | Deep learning[111] uses several layers of neurons between the network's inputs and outputs. The multiple layers can progressively extract higher-level features from the raw input. For example, in image processing, lower layers may identify edges, while higher layers may identify the concepts relevant to a human such as digits or letters or faces.[113] 85 | Deep learning has profoundly improved the performance of programs in many important subfields of artificial intelligence, including computer vision, speech recognition, natural language processing, image classification[114] and others. The reason that deep learning performs so well in so many applications is not known as of 2023.[115] The sudden success of deep learning in 2012–2015 did not occur because of some new discovery or theoretical breakthrough (deep neural networks and backpropagation had been described by many people, as far back as the 1950s)[i] but because of two factors: the incredible increase in computer power (including the hundred-fold increase in speed by switching to GPUs) and the availability of vast amounts of training data, especially the giant curated datasets used for benchmark testing, such as ImageNet.[j] 86 | GPT 87 | Generative pre-trained transformers (GPT) are large language models that are based on the semantic relationships between words in sentences (natural language processing). Text-based GPT models are pre-trained on a large corpus of text which can be from the internet. The pre-training consists in predicting the next token (a token being usually a word, subword, or punctuation). Throughout this pre-training, GPT models accumulate knowledge about the world, and can then generate human-like text by repeatedly predicting the next token. Typically, a subsequent training phase makes the model more truthful, useful and harmless, usually with a technique called reinforcement learning from human feedback (RLHF). Current GPT models are still prone to generating falsehoods called "hallucinations", although this can be reduced with RLHF and quality data. They are used in chatbots, which allow you to ask a question or request a task in simple text.[124][125] 88 | Current models and services include: Gemini (formerly Bard), ChatGPT, Grok, Claude, Copilot and LLaMA.[126] Multimodal GPT models can process different types of data (modalities) such as images, videos, sound and text.[127] 89 | Specialized hardware and software 90 | Main articles: Programming languages for artificial intelligence and Hardware for artificial intelligence 91 | In the late 2010s, graphics processing units (GPUs) that were increasingly designed with AI-specific enhancements and used with specialized TensorFlow software, had replaced previously used central processing unit (CPUs) as the dominant means for large-scale (commercial and academic) machine learning models' training.[128] Historically, specialized languages, such as Lisp, Prolog, Python and others, had been used. 92 | Applications 93 | Main article: Applications of artificial intelligence 94 | AI and machine learning technology is used in most of the essential applications of the 2020s, including: search engines (such as Google Search), targeting online advertisements, recommendation systems (offered by Netflix, YouTube or Amazon), driving internet traffic, targeted advertising (AdSense, Facebook), virtual assistants (such as Siri or Alexa), autonomous vehicles (including drones, ADAS and self-driving cars), automatic language translation (Microsoft Translator, Google Translate), facial recognition (Apple's Face ID or Microsoft's DeepFace and Google's FaceNet) and image labeling (used by Facebook, Apple's iPhoto and TikTok). 95 | Health and medicine 96 | Main article: Artificial intelligence in healthcare 97 | The application of AI in medicine and medical research has the potential to increase patient care and quality of life.[129] Through the lens of the Hippocratic Oath, medical professionals are ethically compelled to use AI, if applications can more accurately diagnose and treat patients. 98 | For medical research, AI is an important tool for processing and integrating big data. This is particularly important for organoid and tissue engineering development which use microscopy imaging as a key technique in fabrication.[130] It has been suggested that AI can overcome discrepancies in funding allocated to different fields of research.[130] New AI tools can deepen our understanding of biomedically relevant pathways. For example, AlphaFold 2 (2021) demonstrated the ability to approximate, in hours rather than months, the 3D structure of a protein.[131] In 2023, it was reported that AI guided drug discovery helped find a class of antibiotics capable of killing two different types of drug-resistant bacteria.[132] 99 | Games 100 | Main article: Game artificial intelligence 101 | Game playing programs have been used since the 1950s to demonstrate and test AI's most advanced techniques.[133] Deep Blue became the first computer chess-playing system to beat a reigning world chess champion, Garry Kasparov, on 11 May 1997.[134] In 2011, in a Jeopardy! quiz show exhibition match, IBM's question answering system, Watson, defeated the two greatest Jeopardy! champions, Brad Rutter and Ken Jennings, by a significant margin.[135] In March 2016, AlphaGo won 4 out of 5 games of Go in a match with Go champion Lee Sedol, becoming the first computer Go-playing system to beat a professional Go player without handicaps. Then in 2017 it defeated Ke Jie, who was the best Go player in the world.[136] Other programs handle imperfect-information games, such as the poker-playing program Pluribus.[137] DeepMind developed increasingly generalistic reinforcement learning models, such as with MuZero, which could be trained to play chess, Go, or Atari games.[138] In 2019, DeepMind's AlphaStar achieved grandmaster level in StarCraft II, a particularly challenging real-time strategy game that involves incomplete knowledge of what happens on the map.[139] In 2021 an AI agent competed in a PlayStation Gran Turismo competition, winning against four of the world's best Gran Turismo drivers using deep reinforcement learning.[140] 102 | Military 103 | Main article: Military artificial intelligence 104 | Various countries are deploying AI military applications.[141] The main applications enhance command and control, communications, sensors, integration and interoperability.[142] Research is targeting intelligence collection and analysis, logistics, cyber operations, information operations, and semiautonomous and autonomous vehicles.[141] AI technologies enable coordination of sensors and effectors, threat detection and identification, marking of enemy positions, target acquisition, coordination and deconfliction of distributed Joint Fires between networked combat vehicles involving manned and unmanned teams.[142] AI was incorporated into military operations in Iraq and Syria.[141] 105 | In November 2023, US Vice President Kamala Harris disclosed a declaration signed by 31 nations to set guardrails for the military use of AI. The commitments include using legal reviews to ensure the compliance of military AI with international laws, and being cautious and transparent in the development of this technology.[143] 106 | Generative AI 107 | Main article: Generative artificial intelligence 108 | 109 | Vincent van Gogh in watercolour created by generative AI software 110 | In the early 2020s, generative AI gained widespread prominence. In March 2023, 58% of US adults had heard about ChatGPT and 14% had tried it.[144] The increasing realism and ease-of-use of AI-based text-to-image generators such as Midjourney, DALL-E, and Stable Diffusion sparked a trend of viral AI-generated photos. Widespread attention was gained by a fake photo of Pope Francis wearing a white puffer coat, the fictional arrest of Donald Trump, and a hoax of an attack on the Pentagon, as well as the usage in professional creative arts.[145][146] 111 | Industry-specific tasks 112 | There are also thousands of successful AI applications used to solve specific problems for specific industries or institutions. In a 2017 survey, one in five companies reported they had incorporated "AI" in some offerings or processes.[147] A few examples are energy storage, medical diagnosis, military logistics, applications that predict the result of judicial decisions, foreign policy, or supply chain management. 113 | In agriculture, AI has helped farmers identify areas that need irrigation, fertilization, pesticide treatments or increasing yield. Agronomists use AI to conduct research and development. AI has been used to predict the ripening time for crops such as tomatoes, monitor soil moisture, operate agricultural robots, conduct predictive analytics, classify livestock pig call emotions, automate greenhouses, detect diseases and pests, and save water. 114 | Artificial intelligence is used in astronomy to analyze increasing amounts of available data and applications, mainly for "classification, regression, clustering, forecasting, generation, discovery, and the development of new scientific insights" for example for discovering exoplanets, forecasting solar activity, and distinguishing between signals and instrumental effects in gravitational wave astronomy. It could also be used for activities in space such as space exploration, including analysis of data from space missions, real-time science decisions of spacecraft, space debris avoidance, and more autonomous operation. 115 | Ethics 116 | Main article: Ethics of artificial intelligence 117 | AI has potential benefits and potential risks. AI may be able to advance science and find solutions for serious problems: Demis Hassabis of Deep Mind hopes to "solve intelligence, and then use that to solve everything else".[148] However, as the use of AI has become widespread, several unintended consequences and risks have been identified.[149] In-production systems can sometimes not factor ethics and bias into their AI training processes, especially when the AI algorithms are inherently unexplainable in deep learning.[150] 118 | Risks and harm 119 | Privacy and copyright 120 | Further information: Information privacy and Artificial intelligence and copyright 121 | Machine-learning algorithms require large amounts of data. The techniques used to acquire this data have raised concerns about privacy, surveillance and copyright. 122 | Technology companies collect a wide range of data from their users, including online activity, geolocation data, video and audio.[151] For example, in order to build speech recognition algorithms, Amazon has recorded millions of private conversations and allowed temporary workers to listen to and transcribe some of them.[152] Opinions about this widespread surveillance range from those who see it as a necessary evil to those for whom it is clearly unethical and a violation of the right to privacy.[153] 123 | AI developers argue that this is the only way to deliver valuable applications. and have developed several techniques that attempt to preserve privacy while still obtaining the data, such as data aggregation, de-identification and differential privacy.[154] Since 2016, some privacy experts, such as Cynthia Dwork, have begun to view privacy in terms of fairness. Brian Christian wrote that experts have pivoted "from the question of 'what they know' to the question of 'what they're doing with it'."[155] 124 | Generative AI is often trained on unlicensed copyrighted works, including in domains such as images or computer code; the output is then used under the rationale of "fair use". Website owners who do not wish to have their copyrighted content AI-indexed or 'scraped' can add code to their site if they do not want their website to be indexed by a search engine, which is currently available through certain services such as OpenAI. Experts disagree about how well and under what circumstances this rationale will hold up in courts of law; relevant factors may include "the purpose and character of the use of the copyrighted work" and "the effect upon the potential market for the copyrighted work".[156] In 2023, leading authors (including John Grisham and Jonathan Franzen) sued AI companies for using their work to train generative AI.[157][158] 125 | Misinformation 126 | See also: YouTube § Moderation and offensive content 127 | YouTube, Facebook and others use recommender systems to guide users to more content. These AI programs were given the goal of maximizing user engagement (that is, the only goal was to keep people watching). The AI learned that users tended to choose misinformation, conspiracy theories, and extreme partisan content, and, to keep them watching, the AI recommended more of it. Users also tended to watch more content on the same subject, so the AI led people into filter bubbles where they received multiple versions of the same misinformation.[159] This convinced many users that the misinformation was true, and ultimately undermined trust in institutions, the media and the government.[160] The AI program had correctly learned to maximize its goal, but the result was harmful to society. After the U.S. election in 2016, major technology companies took steps to mitigate the problem. 128 | In 2022, generative AI began to create images, audio, video and text that are indistinguishable from real photographs, recordings, films or human writing. It is possible for bad actors to use this technology to create massive amounts of misinformation or propaganda.[161] AI pioneer Geoffrey Hinton expressed concern about AI enabling "authoritarian leaders to manipulate their electorates" on a large scale, among other risks.[162] 129 | Algorithmic bias and fairness 130 | Main articles: Algorithmic bias and Fairness (machine learning) 131 | Machine learning applications will be biased if they learn from biased data.[163] The developers may not be aware that the bias exists.[164] Bias can be introduced by the way training data is selected and by the way a model is deployed.[165][163] If a biased algorithm is used to make decisions that can seriously harm people (as it can in medicine, finance, recruitment, housing or policing) then the algorithm may cause discrimination.[166] Fairness in machine learning is the study of how to prevent the harm caused by algorithmic bias. It has become serious area of academic study within AI. Researchers have discovered it is not always possible to define "fairness" in a way that satisfies all stakeholders.[167] 132 | On June 28, 2015, Google Photos's new image labeling feature mistakenly identified Jacky Alcine and a friend as "gorillas" because they were black. The system was trained on a dataset that contained very few images of black people,[168] a problem called "sample size disparity".[169] Google "fixed" this problem by preventing the system from labelling anything as a "gorilla". Eight years later, in 2023, Google Photos still could not identify a gorilla, and neither could similar products from Apple, Facebook, Microsoft and Amazon.[170] 133 | COMPAS is a commercial program widely used by U.S. courts to assess the likelihood of a defendant becoming a recidivist. In 2016, Julia Angwin at ProPublica discovered that COMPAS exhibited racial bias, despite the fact that the program was not told the races of the defendants. Although the error rate for both whites and blacks was calibrated equal at exactly 61%, the errors for each race were different—the system consistently overestimated the chance that a black person would re-offend and would underestimate the chance that a white person would not re-offend.[171] In 2017, several researchers[k] showed that it was mathematically impossible for COMPAS to accommodate all possible measures of fairness when the base rates of re-offense were different for whites and blacks in the data.[173] 134 | A program can make biased decisions even if the data does not explicitly mention a problematic feature (such as "race" or "gender"). The feature will correlate with other features (like "address", "shopping history" or "first name"), and the program will make the same decisions based on these features as it would on "race" or "gender".[174] Moritz Hardt said "the most robust fact in this research area is that fairness through blindness doesn't work."[175] 135 | Criticism of COMPAS highlighted that machine learning models are designed to make "predictions" that are only valid if we assume that the future will resemble the past. If they are trained on data that includes the results of racist decisions in the past, machine learning models must predict that racist decisions will be made in the future. If an application then uses these predictions as recommendations, some of these "recommendations" will likely be racist.[176] Thus, machine learning is not well suited to help make decisions in areas where there is hope that the future will be better than the past. It is necessarily descriptive and not proscriptive.[l] 136 | Bias and unfairness may go undetected because the developers are overwhelmingly white and male: among AI engineers, about 4% are black and 20% are women.[169] 137 | At its 2022 Conference on Fairness, Accountability, and Transparency (ACM FAccT 2022), the Association for Computing Machinery, in Seoul, South Korea, presented and published findings that recommend that until AI and robotics systems are demonstrated to be free of bias mistakes, they are unsafe, and the use of self-learning neural networks trained on vast, unregulated sources of flawed internet data should be curtailed.[178] 138 | Lack of transparency 139 | See also: Explainable AI, Algorithmic transparency, and Right to explanation 140 | 141 | Lidar testing vehicle for autonomous driving 142 | Many AI systems are so complex that their designers cannot explain how they reach their decisions.[179] Particularly with deep neural networks, in which there are a large amount of non-linear relationships between inputs and outputs. But some popular explainability techniques exist.[180] 143 | It is impossible to be certain that a program is operating correctly if no one knows how exactly it works. There have been many cases where a machine learning program passed rigorous tests, but nevertheless learned something different than what the programmers intended. For example, a system that could identify skin diseases better than medical professionals was found to actually have a strong tendency to classify images with a ruler as "cancerous", because pictures of malignancies typically include a ruler to show the scale.[181] Another machine learning system designed to help effectively allocate medical resources was found to classify patients with asthma as being at "low risk" of dying from pneumonia. Having asthma is actually a severe risk factor, but since the patients having asthma would usually get much more medical care, they were relatively unlikely to die according to the training data. The correlation between asthma and low risk of dying from pneumonia was real, but misleading.[182] 144 | People who have been harmed by an algorithm's decision have a right to an explanation.[183] Doctors, for example, are expected to clearly and completely explain to their colleagues the reasoning behind any decision they make. Early drafts of the European Union's General Data Protection Regulation in 2016 included an explicit statement that this right exists.[m] Industry experts noted that this is an unsolved problem with no solution in sight. Regulators argued that nevertheless the harm is real: if the problem has no solution, the tools should not be used.[184] 145 | DARPA established the XAI ("Explainable Artificial Intelligence") program in 2014 to try and solve these problems.[185] 146 | There are several possible solutions to the transparency problem. SHAP tried to solve the transparency problems by visualising the contribution of each feature to the output.[186] LIME can locally approximate a model with a simpler, interpretable model.[187] Multitask learning provides a large number of outputs in addition to the target classification. These other outputs can help developers deduce what the network has learned.[188] Deconvolution, DeepDream and other generative methods can allow developers to see what different layers of a deep network have learned and produce output that can suggest what the network is learning.[189] 147 | Bad actors and weaponized AI 148 | Main articles: Lethal autonomous weapon, Artificial intelligence arms race, and AI safety 149 | Artificial intelligence provides a number of tools that are useful to bad actors, such as authoritarian governments, terrorists, criminals or rogue states. 150 | A lethal autonomous weapon is a machine that locates, selects and engages human targets without human supervision.[n] Widely available AI tools can be used by bad actors to develop inexpensive autonomous weapons and, if produced at scale, they are potentially weapons of mass destruction.[191] Even when used in conventional warfare, it is unlikely that they will be unable to reliably choose targets and could potentially kill an innocent person.[191] In 2014, 30 nations (including China) supported a ban on autonomous weapons under the United Nations' Convention on Certain Conventional Weapons, however the United States and others disagreed.[192] By 2015, over fifty countries were reported to be researching battlefield robots.[193] 151 | AI tools make it easier for authoritarian governments to efficiently control their citizens in several ways. Face and voice recognition allow widespread surveillance. Machine learning, operating this data, can classify potential enemies of the state and prevent them from hiding. Recommendation systems can precisely target propaganda and misinformation for maximum effect. Deepfakes and generative AI aid in producing misinformation. Advanced AI can make authoritarian centralized decision making more competitive than liberal and decentralized systems such as markets. It lowers the cost and difficulty of digital warfare and advanced spyware.[194] All these technologies have been available since 2020 or earlier -- AI facial recognition systems are already being used for mass surveillance in China.[195][196] 152 | There many other ways that AI is expected to help bad actors, some of which can not be foreseen. For example, machine-learning AI is able to design tens of thousands of toxic molecules in a matter of hours.[197] 153 | Reliance on industry giants 154 | Training AI systems requires an enormous amount of computing power. Usually only Big Tech companies have the financial resources to make such investments. Smaller startups such as Cohere and OpenAI end up buying access to data centers from Google and Microsoft respectively.[198] 155 | Technological unemployment 156 | Main articles: Workplace impact of artificial intelligence and Technological unemployment 157 | Economists have frequently highlighted the risks of redundancies from AI, and speculated about unemployment if there is no adequate social policy for full employment.[199] 158 | In the past, technology has tended to increase rather than reduce total employment, but economists acknowledge that "we're in uncharted territory" with AI.[200] A survey of economists showed disagreement about whether the increasing use of robots and AI will cause a substantial increase in long-term unemployment, but they generally agree that it could be a net benefit if productivity gains are redistributed.[201] Risk estimates vary; for example, in the 2010s, Michael Osborne and Carl Benedikt Frey estimated 47% of U.S. jobs are at "high risk" of potential automation, while an OECD report classified only 9% of U.S. jobs as "high risk".[o][203] The methodology of speculating about future employment levels has been criticised as lacking evidential foundation, and for implying that technology, rather than social policy, creates unemployment, as opposed to redundancies.[199] In April 2023, it was reported that 70% of the jobs for Chinese video game illustrators had been eliminated by generative artificial intelligence.[204][205] 159 | Unlike previous waves of automation, many middle-class jobs may be eliminated by artificial intelligence; The Economist stated in 2015 that "the worry that AI could do to white-collar jobs what steam power did to blue-collar ones during the Industrial Revolution" is "worth taking seriously".[206] Jobs at extreme risk range from paralegals to fast food cooks, while job demand is likely to increase for care-related professions ranging from personal healthcare to the clergy.[207] 160 | From the early days of the development of artificial intelligence, there have been arguments, for example, those put forward by Joseph Weizenbaum, about whether tasks that can be done by computers actually should be done by them, given the difference between computers and humans, and between quantitative calculation and qualitative, value-based judgement.[208] 161 | Existential risk 162 | Main article: Existential risk from artificial general intelligence 163 | It has been argued AI will become so powerful that humanity may irreversibly lose control of it. This could, as physicist Stephen Hawking stated, "spell the end of the human race".[209] This scenario has been common in science fiction, when a computer or robot suddenly develops a human-like "self-awareness" (or "sentience" or "consciousness") and becomes a malevolent character.[p] These sci-fi scenarios are misleading in several ways. 164 | First, AI does not require human-like "sentience" to be an existential risk. Modern AI programs are given specific goals and use learning and intelligence to achieve them. Philosopher Nick Bostrom argued that if one gives almost any goal to a sufficiently powerful AI, it may choose to destroy humanity to achieve it (he used the example of a paperclip factory manager).[211] Stuart Russell gives the example of household robot that tries to find a way to kill its owner to prevent it from being unplugged, reasoning that "you can't fetch the coffee if you're dead."[212] In order to be safe for humanity, a superintelligence would have to be genuinely aligned with humanity's morality and values so that it is "fundamentally on our side".[213] 165 | Second, Yuval Noah Harari argues that AI does not require a robot body or physical control to pose an existential risk. The essential parts of civilization are not physical. Things like ideologies, law, government, money and the economy are made of language; they exist because there are stories that billions of people believe. The current prevalence of misinformation suggests that an AI could use language to convince people to believe anything, even to take actions that are destructive.[214] 166 | The opinions amongst experts and industry insiders are mixed, with sizable fractions both concerned and unconcerned by risk from eventual superintelligent AI.[215] Personalities such as Stephen Hawking, Bill Gates, and Elon Musk have expressed concern about existential risk from AI.[216] AI pioneers including Fei-Fei Li, Geoffrey Hinton, Yoshua Bengio, Cynthia Breazeal, Rana el Kaliouby, Demis Hassabis, Joy Buolamwini, and Sam Altman have expressed concerns about the risks of AI. In 2023, many leading AI experts issued the joint statement that "Mitigating the risk of extinction from AI should be a global priority alongside other societal-scale risks such as pandemics and nuclear war".[217] 167 | Other researchers, however, spoke in favor of a less dystopian view. AI pioneer Juergen Schmidhuber did not sign the joint statement, emphasising that in 95% of all cases, AI research is about making "human lives longer and healthier and easier."[218] While the tools that are now being used to improve lives can also be used by bad actors, "they can also be used against the bad actors."[219][220] Andrew Ng also argued that "it's a mistake to fall for the doomsday hype on AI—and that regulators who do will only benefit vested interests."[221] Yann LeCun "scoffs at his peers' dystopian scenarios of supercharged misinformation and even, eventually, human extinction."[222] In the early 2010s, experts argued that the risks are too distant in the future to warrant research or that humans will be valuable from the perspective of a superintelligent machine.[223] However, after 2016, the study of current and future risks and possible solutions became a serious area of research.[224] 168 | Ethical machines and alignment 169 | Main articles: Machine ethics, AI safety, Friendly artificial intelligence, Artificial moral agents, and Human Compatible 170 | Friendly AI are machines that have been designed from the beginning to minimize risks and to make choices that benefit humans. Eliezer Yudkowsky, who coined the term, argues that developing friendly AI should be a higher research priority: it may require a large investment and it must be completed before AI becomes an existential risk.[225] 171 | Machines with intelligence have the potential to use their intelligence to make ethical decisions. The field of machine ethics provides machines with ethical principles and procedures for resolving ethical dilemmas.[226] The field of machine ethics is also called computational morality,[226] and was founded at an AAAI symposium in 2005.[227] 172 | Other approaches include Wendell Wallach's "artificial moral agents"[228] and Stuart J. Russell's three principles for developing provably beneficial machines.[229] 173 | Frameworks 174 | Artificial Intelligence projects can have their ethical permissibility tested while designing, developing, and implementing an AI system. An AI framework such as the Care and Act Framework containing the SUM values—developed by the Alan Turing Institute tests projects in four main areas:[230][231] 175 | RESPECT the dignity of individual people 176 | CONNECT with other people sincerely, openly and inclusively 177 | CARE for the wellbeing of everyone 178 | PROTECT social values, justice and the public interest 179 | Other developments in ethical frameworks include those decided upon during the Asilomar Conference, the Montreal Declaration for Responsible AI, and the IEEE's Ethics of Autonomous Systems initiative, among others;[232] however, these principles do not go without their criticisms, especially regards to the people chosen contributes to these frameworks.[233] 180 | Promotion of the wellbeing of the people and communities that these technologies affect requires consideration of the social and ethical implications at all stages of AI system design, development and implementation, and collaboration between job roles such as data scientists, product managers, data engineers, domain experts, and delivery managers.[234] 181 | Regulation 182 | Main articles: Regulation of artificial intelligence, Regulation of algorithms, and AI safety 183 | 184 | The first global AI Safety Summit was held in 2023 with a declaration calling for international co-operation. 185 | The regulation of artificial intelligence is the development of public sector policies and laws for promoting and regulating artificial intelligence (AI); it is therefore related to the broader regulation of algorithms.[235] The regulatory and policy landscape for AI is an emerging issue in jurisdictions globally.[236] According to AI Index at Stanford, the annual number of AI-related laws passed in the 127 survey countries jumped from one passed in 2016 to 37 passed in 2022 alone.[237][238] Between 2016 and 2020, more than 30 countries adopted dedicated strategies for AI.[239] Most EU member states had released national AI strategies, as had Canada, China, India, Japan, Mauritius, the Russian Federation, Saudi Arabia, United Arab Emirates, US and Vietnam. Others were in the process of elaborating their own AI strategy, including Bangladesh, Malaysia and Tunisia.[239] The Global Partnership on Artificial Intelligence was launched in June 2020, stating a need for AI to be developed in accordance with human rights and democratic values, to ensure public confidence and trust in the technology.[239] Henry Kissinger, Eric Schmidt, and Daniel Huttenlocher published a joint statement in November 2021 calling for a government commission to regulate AI.[240] In 2023, OpenAI leaders published recommendations for the governance of superintelligence, which they believe may happen in less than 10 years.[241] In 2023, the United Nations also launched an advisory body to provide recommendations on AI governance; the body comprises technology company executives, governments officials and academics.[242] 186 | In a 2022 Ipsos survey, attitudes towards AI varied greatly by country; 78% of Chinese citizens, but only 35% of Americans, agreed that "products and services using AI have more benefits than drawbacks".[237] A 2023 Reuters/Ipsos poll found that 61% of Americans agree, and 22% disagree, that AI poses risks to humanity.[243] In a 2023 Fox News poll, 35% of Americans thought it "very important", and an additional 41% thought it "somewhat important", for the federal government to regulate AI, versus 13% responding "not very important" and 8% responding "not at all important".[244][245] 187 | In November 2023, the first global AI Safety Summit was held in Bletchley Park in the UK to discuss the near and far term risks of AI and the possibility of mandatory and voluntary regulatory frameworks.[246] 28 countries including the United States, China, and the European Union issued a declaration at the start of the summit, calling for international co-operation to manage the challenges and risks of artificial intelligence.[247][248] 188 | History 189 | Main article: History of artificial intelligence 190 | For a chronological guide, see Timeline of artificial intelligence. 191 | The study of mechanical or "formal" reasoning began with philosophers and mathematicians in antiquity. The study of logic led directly to Alan Turing's theory of computation, which suggested that a machine, by shuffling symbols as simple as "0" and "1", could simulate any conceivable form of mathematical reasoning.[249][5] This, along with concurrent discoveries in cybernetics, information theory and neurobiology, led researchers to consider the possibility of building an "electronic brain".[q] They developed several areas of research that would become part of AI,[251] such as McCullouch and Pitts design for "artificial neurons" in 1943,[252] and Turing's influential 1950 paper 'Computing Machinery and Intelligence', which introduced the Turing test and showed that "machine intelligence" was plausible.[253][5] 192 | The field of AI research was founded at a workshop at Dartmouth College in 1956.[r][6] The attendees became the leaders of AI research in the 1960s.[s] They and their students produced programs that the press described as "astonishing":[t] computers were learning checkers strategies, solving word problems in algebra, proving logical theorems and speaking English.[u][7] Artificial intelligence laboratories were set up at a number of British and U.S. Universities in the latter 1950s and early 1960s.[5] 193 | Researchers in the 1960s and the 1970s were convinced that their methods would eventually succeed in creating a machine with general intelligence and considered this the goal of their field.[257] Herbert Simon predicted, "machines will be capable, within twenty years, of doing any work a man can do".[258] Marvin Minsky agreed, writing, "within a generation ... the problem of creating 'artificial intelligence' will substantially be solved".[259] They had, however, underestimated the difficulty of the problem.[v] In 1974, both the U.S. and British governments cut off exploratory research in response to the criticism of Sir James Lighthill[261] and ongoing pressure from the U.S. Congress to fund more productive projects.[262] Minsky's and Papert's book Perceptrons was understood as proving that artificial neural networks would never be useful for solving real-world tasks, thus discrediting the approach altogether.[263] The "AI winter", a period when obtaining funding for AI projects was difficult, followed.[9] 194 | In the early 1980s, AI research was revived by the commercial success of expert systems,[264] a form of AI program that simulated the knowledge and analytical skills of human experts. By 1985, the market for AI had reached over a billion dollars. At the same time, Japan's fifth generation computer project inspired the U.S. and British governments to restore funding for academic research.[8] However, beginning with the collapse of the Lisp Machine market in 1987, AI once again fell into disrepute, and a second, longer-lasting winter began.[10] 195 | Up to this point, most of AI's funding had gone to projects which used high level symbols to represent mental objects like plans, goals, beliefs and known facts. In the 1980s, some researchers began to doubt that this approach would be able to imitate all the processes of human cognition, especially perception, robotics, learning and pattern recognition,[265] and began to look into "sub-symbolic" approaches.[266] Rodney Brooks rejected "representation" in general and focussed directly on engineering machines that move and survive.[w] Judea Pearl, Lofti Zadeh and others developed methods that handled incomplete and uncertain information by making reasonable guesses rather than precise logic.[88][271] But the most important development was the revival of "connectionism", including neural network research, by Geoffrey Hinton and others.[272] In 1990, Yann LeCun successfully showed that convolutional neural networks can recognize handwritten digits, the first of many successful applications of neural networks.[273] 196 | AI gradually restored its reputation in the late 1990s and early 21st century by exploiting formal mathematical methods and by finding specific solutions to specific problems. This "narrow" and "formal" focus allowed researchers to produce verifiable results and collaborate with other fields (such as statistics, economics and mathematics).[274] By 2000, solutions developed by AI researchers were being widely used, although in the 1990s they were rarely described as "artificial intelligence".[275] However, several academic researchers became concerned that AI was no longer pursuing its original goal of creating versatile, fully intelligent machines. Beginning around 2002, they founded the subfield of artificial general intelligence (or "AGI"), which had several well-funded institutions by the 2010s.[14] 197 | Deep learning began to dominate industry benchmarks in 2012 and was adopted throughout the field.[11] For many specific tasks, other methods were abandoned.[x] Deep learning's success was based on both hardware improvements (faster computers,[277] graphics processing units, cloud computing[278]) and access to large amounts of data[279] (including curated datasets,[278] such as ImageNet). Deep learning's success led to an enormous increase in interest and funding in AI.[y] The amount of machine learning research (measured by total publications) increased by 50% in the years 2015–2019.[239] 198 | In 2016, issues of fairness and the misuse of technology were catapulted into center stage at machine learning conferences, publications vastly increased, funding became available, and many researchers re-focussed their careers on these issues. The alignment problem became a serious field of academic study.[224] 199 | In the late teens and early 2020s, AGI companies began to deliver programs that created enormous interest. In 2015, AlphaGo, developed by DeepMind, beat the world champion Go player. The program was taught only the rules of the game and developed strategy by itself. GPT-3 is a large language model that was released in 2020 by OpenAI and is capable of generating high-quality human-like text.[280] These programs, and others, inspired an aggressive AI boom, where large companies began investing billions in AI research. According to 'AI Impacts', about $50 billion annually was invested in "AI" around 2022 in the U.S. alone and about 20% of new US Computer Science PhD graduates have specialized in "AI".[281] About 800,000 "AI"-related US job openings existed in 2022.[282] 200 | Philosophy 201 | Main article: Philosophy of artificial intelligence 202 | Defining artificial intelligence 203 | Main articles: Turing test, Intelligent agent, Dartmouth workshop, and Synthetic intelligence 204 | Alan Turing wrote in 1950 "I propose to consider the question 'can machines think'?"[283] He advised changing the question from whether a machine "thinks", to "whether or not it is possible for machinery to show intelligent behaviour".[283] He devised the Turing test, which measures the ability of a machine to simulate human conversation.[253] Since we can only observe the behavior of the machine, it does not matter if it is "actually" thinking or literally has a "mind". Turing notes that we can not determine these things about other people but "it is usual to have a polite convention that everyone thinks"[284] 205 | Russell and Norvig agree with Turing that intelligence must be defined in terms of external behavior, not internal structure.[1] However, they are critical that the test requires the machine to imitate humans. "Aeronautical engineering texts," they wrote, "do not define the goal of their field as making 'machines that fly so exactly like pigeons that they can fool other pigeons.'"[285] AI founder John McCarthy agreed, writing that "Artificial intelligence is not, by definition, simulation of human intelligence".[286] 206 | McCarthy defines intelligence as "the computational part of the ability to achieve goals in the world."[287] Another AI founder, Marvin Minsky similarly describes it as "the ability to solve hard problems".[288] The leading AI textbook defines it as the study of agents that perceive their environment and take actions that maximize their chances of achieving defined goals.[289] These definitions view intelligence in terms of well-defined problems with well-defined solutions, where both the difficulty of the problem and the performance of the program are direct measures of the "intelligence" of the machine—and no other philosophical discussion is required, or may not even be possible. 207 | Another definition has been adopted by Google,[290] a major practitioner in the field of AI. This definition stipulates the ability of systems to synthesize information as the manifestation of intelligence, similar to the way it is defined in biological intelligence. 208 | Evaluating approaches to AI 209 | No established unifying theory or paradigm has guided AI research for most of its history.[z] The unprecedented success of statistical machine learning in the 2010s eclipsed all other approaches (so much so that some sources, especially in the business world, use the term "artificial intelligence" to mean "machine learning with neural networks"). This approach is mostly sub-symbolic, soft and narrow (see below). Critics argue that these questions may have to be revisited by future generations of AI researchers. 210 | Symbolic AI and its limits 211 | Symbolic AI (or "GOFAI")[292] simulated the high-level conscious reasoning that people use when they solve puzzles, express legal reasoning and do mathematics. They were highly successful at "intelligent" tasks such as algebra or IQ tests. In the 1960s, Newell and Simon proposed the physical symbol systems hypothesis: "A physical symbol system has the necessary and sufficient means of general intelligent action."[293] 212 | However, the symbolic approach failed on many tasks that humans solve easily, such as learning, recognizing an object or commonsense reasoning. Moravec's paradox is the discovery that high-level "intelligent" tasks were easy for AI, but low level "instinctive" tasks were extremely difficult.[294] Philosopher Hubert Dreyfus had argued since the 1960s that human expertise depends on unconscious instinct rather than conscious symbol manipulation, and on having a "feel" for the situation, rather than explicit symbolic knowledge.[295] Although his arguments had been ridiculed and ignored when they were first presented, eventually, AI research came to agree with him.[aa][19] 213 | The issue is not resolved: sub-symbolic reasoning can make many of the same inscrutable mistakes that human intuition does, such as algorithmic bias. Critics such as Noam Chomsky argue continuing research into symbolic AI will still be necessary to attain general intelligence,[297][298] in part because sub-symbolic AI is a move away from explainable AI: it can be difficult or impossible to understand why a modern statistical AI program made a particular decision. The emerging field of neuro-symbolic artificial intelligence attempts to bridge the two approaches. 214 | Neat vs. scruffy 215 | Main article: Neats and scruffies 216 | "Neats" hope that intelligent behavior is described using simple, elegant principles (such as logic, optimization, or neural networks). "Scruffies" expect that it necessarily requires solving a large number of unrelated problems. Neats defend their programs with theoretical rigor, scruffies rely mainly on incremental testing to see if they work. This issue was actively discussed in the 1970s and 1980s,[299] but eventually was seen as irrelevant. Modern AI has elements of both. 217 | Soft vs. hard computing 218 | Main article: Soft computing 219 | Finding a provably correct or optimal solution is intractable for many important problems.[18] Soft computing is a set of techniques, including genetic algorithms, fuzzy logic and neural networks, that are tolerant of imprecision, uncertainty, partial truth and approximation. Soft computing was introduced in the late 1980s and most successful AI programs in the 21st century are examples of soft computing with neural networks. 220 | Narrow vs. general AI 221 | Main articles: Weak artificial intelligence and Artificial general intelligence 222 | AI researchers are divided as to whether to pursue the goals of artificial general intelligence and superintelligence directly or to solve as many specific problems as possible (narrow AI) in hopes these solutions will lead indirectly to the field's long-term goals.[300][301] General intelligence is difficult to define and difficult to measure, and modern AI has had more verifiable successes by focusing on specific problems with specific solutions. The experimental sub-field of artificial general intelligence studies this area exclusively. 223 | Machine consciousness, sentience and mind 224 | Main articles: Philosophy of artificial intelligence and Artificial consciousness 225 | The philosophy of mind does not know whether a machine can have a mind, consciousness and mental states, in the same sense that human beings do. This issue considers the internal experiences of the machine, rather than its external behavior. Mainstream AI research considers this issue irrelevant because it does not affect the goals of the field: to build machines that can solve problems using intelligence. Russell and Norvig add that "[t]he additional project of making a machine conscious in exactly the way humans are is not one that we are equipped to take on."[302] However, the question has become central to the philosophy of mind. It is also typically the central question at issue in artificial intelligence in fiction. 226 | Consciousness 227 | Main articles: Hard problem of consciousness and Theory of mind 228 | David Chalmers identified two problems in understanding the mind, which he named the "hard" and "easy" problems of consciousness.[303] The easy problem is understanding how the brain processes signals, makes plans and controls behavior. The hard problem is explaining how this feels or why it should feel like anything at all, assuming we are right in thinking that it truly does feel like something (Dennett's consciousness illusionism says this is an illusion). Human information processing is easy to explain, however, human subjective experience is difficult to explain. For example, it is easy to imagine a color-blind person who has learned to identify which objects in their field of view are red, but it is not clear what would be required for the person to know what red looks like.[304] 229 | Computationalism and functionalism 230 | Main articles: Computational theory of mind, Functionalism (philosophy of mind), and Chinese room 231 | Computationalism is the position in the philosophy of mind that the human mind is an information processing system and that thinking is a form of computing. Computationalism argues that the relationship between mind and body is similar or identical to the relationship between software and hardware and thus may be a solution to the mind–body problem. This philosophical position was inspired by the work of AI researchers and cognitive scientists in the 1960s and was originally proposed by philosophers Jerry Fodor and Hilary Putnam.[305] 232 | Philosopher John Searle characterized this position as "strong AI": "The appropriately programmed computer with the right inputs and outputs would thereby have a mind in exactly the same sense human beings have minds."[ab] Searle counters this assertion with his Chinese room argument, which attempts to show that, even if a machine perfectly simulates human behavior, there is still no reason to suppose it also has a mind.[309] 233 | AI welfare and rights 234 | It is difficult or impossible to reliably evaluate whether an advanced AI is sentient (has the ability to feel), and if so, to what degree.[310] But if there is a significant chance that a given machine can feel and suffer, then it may be entitled to certain rights or welfare protection measures, similarly to animals.[311][312] Sapience (a set of capacities related to high intelligence, such as discernment or self-awareness) may provide another moral basis for AI rights.[311] Robot rights are also sometimes proposed as a practical way to integrate autonomous agents into society.[313] 235 | In 2017, the European Union considered granting "electronic personhood" to some of the most capable AI systems. Similarly to the legal status of companies, it would have conferred rights but also responsibilities.[314] Critics argued in 2018 that granting rights to AI systems would downplay the importance of human rights, and that legislation should focus on user needs rather than speculative futuristic scenarios. They also noted that robots lacked the autonomy to take part to society on their own.[315][316] 236 | Progress in AI increased interest in the topic. Proponents of AI welfare and rights often argue that AI sentience, if it emerges, would be particularly easy to deny. They warn that this may be a moral blind spot analogous to slavery or factory farming, which could lead to large-scale suffering if sentient AI is created and carelessly exploited.[312][311] 237 | Future 238 | Superintelligence and the singularity 239 | A superintelligence is a hypothetical agent that would possess intelligence far surpassing that of the brightest and most gifted human mind.[301] 240 | If research into artificial general intelligence produced sufficiently intelligent software, it might be able to reprogram and improve itself. The improved software would be even better at improving itself, leading to what I. J. Good called an "intelligence explosion" and Vernor Vinge called a "singularity".[317] 241 | However, technologies cannot improve exponentially indefinitely, and typically follow an S-shaped curve, slowing when they reach the physical limits of what the technology can do.[318] 242 | Transhumanism 243 | Robot designer Hans Moravec, cyberneticist Kevin Warwick, and inventor Ray Kurzweil have predicted that humans and machines will merge in the future into cyborgs that are more capable and powerful than either. This idea, called transhumanism, has roots in Aldous Huxley and Robert Ettinger.[319] 244 | Edward Fredkin argues that "artificial intelligence is the next stage in evolution", an idea first proposed by Samuel Butler's "Darwin among the Machines" as far back as 1863, and expanded upon by George Dyson in his book of the same name in 1998.[320] 245 | In fiction 246 | Main article: Artificial intelligence in fiction 247 | 248 | The word "robot" itself was coined by Karel Čapek in his 1921 play R.U.R., the title standing for "Rossum's Universal Robots". 249 | Thought-capable artificial beings have appeared as storytelling devices since antiquity,[321] and have been a persistent theme in science fiction.[322] 250 | A common trope in these works began with Mary Shelley's Frankenstein, where a human creation becomes a threat to its masters. This includes such works as Arthur C. Clarke's and Stanley Kubrick's 2001: A Space Odyssey (both 1968), with HAL 9000, the murderous computer in charge of the Discovery One spaceship, as well as The Terminator (1984) and The Matrix (1999). In contrast, the rare loyal robots such as Gort from The Day the Earth Stood Still (1951) and Bishop from Aliens (1986) are less prominent in popular culture.[323] 251 | Isaac Asimov introduced the Three Laws of Robotics in many books and stories, most notably the "Multivac" series about a super-intelligent computer of the same name. Asimov's laws are often brought up during lay discussions of machine ethics;[324] while almost all artificial intelligence researchers are familiar with Asimov's laws through popular culture, they generally consider the laws useless for many reasons, one of which is their ambiguity.[325] 252 | Several works use AI to force us to confront the fundamental question of what makes us human, showing us artificial beings that have the ability to feel, and thus to suffer. This appears in Karel Čapek's R.U.R., the films A.I. Artificial Intelligence and Ex Machina, as well as the novel Do Androids Dream of Electric Sheep?, by Philip K. Dick. Dick considers the idea that our understanding of human subjectivity is altered by technology created with artificial intelligence. 253 | --------------------------------------------------------------------------------