Bruno Marchand’s Post

View profile for Bruno Marchand, graphic

Global Center of Excellence Manager @ Sage | Strategic Leadership

Calling AI APIs with JavaScript: A Simple Example (Soon with your Center of Excellence: October 2024) Artificial intelligence (AI) is revolutionizing the way we interact with technology, and AI APIs are making it easier than ever to integrate AI into our applications. In this post, I'll show you how to call an AI API using the JavaScript fetch API. Here's an example of how to call the OpenAI API's text completion endpoint and ask for a text about IT business challenges: JavaScript const fetch = require('node-fetch'); const API_ENDPOINT = 'https://lnkd.in/eF9jZxdA'; const API_KEY = 'YOUR_API_KEY'; const prompt = 'Write a text related to IT business challenges.'; const headers = { 'Authorization': `Bearer ${API_KEY}`, 'Content-Type': 'application/x-www-form-urlencoded', }; const query = new URLSearchParams(); query.set('prompt', prompt); query.set('max_tokens', 256); query.set('temperature', 0.7); const requestOptions = { method: 'POST', headers: headers, body: query.toString(), }; fetch(API_ENDPOINT, requestOptions) .then(response => response.json()) .then(data => { if (data.choices.length === 0) { throw new Error('No completions returned'); } const completion = data.choices[0].text; console.log(completion); }) .catch(error => { console.error(error); }); This script will do the following: - Call the OpenAI API's text completion endpoint - Write a text related to IT business challenges - Return the generated text Here's a breakdown of the script: - Import the fetch library: This library is used to make HTTP requests. - Define the API endpoint: This is the URL of the OpenAI API endpoint that we want to call. - Define your API key: This is the key that allows you to use the OpenAI API. - Define the prompt: This is the text that you want the AI to generate a completion for. - Create headers: These headers are required to authenticate with the OpenAI API. - Create query parameters: These parameters provide additional information to the AI API. - Create request options: These options specify the type of request and the data to send. - Make the request: This sends the request to the API and returns a response. - Parse the response: This extracts the data from the response and converts it into a JavaScript object. - Check for errors: This throws an error if there are any problems with the response. - Get the completion: This extracts the generated text from the response object. - Print the completion: This prints the generated text to the console. This script will call the OpenAI API's text completion endpoint, but it will ask for information about a business instead of generating a poem. The AI will return a completion with a maximum of 256 tokens and a temperature of 0.7. The temperature parameter controls the creativity of the generated text, with higher values resulting in more creative but potentially less coherent text. #sageuniversity #sagepartner

  • No alternative text description for this image

To view or add a comment, sign in

Explore topics