Created
November 13, 2023 22:53
-
-
Save Lhemamou/679f6cc817abe5c5c595e703edf82fcc to your computer and use it in GitHub Desktop.
Performs a basic Word API call using TypeScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Basic API call (TypeScript) | |
description: Performs a basic Word API call using TypeScript. | |
host: WORD | |
api_set: {} | |
script: | |
content: > | |
document.getElementById("run")!.addEventListener("click", () => | |
tryCatch(run)); | |
async function run() { | |
await Word.run(async (context) => { | |
// Get the prompt from the text input box | |
const promptInput = document.getElementById("promptInput") as HTMLInputElement; | |
const promptText = promptInput.value; | |
// Get the currently selected text in the Word document | |
const range = context.document.getSelection(); | |
range.load("text"); | |
await context.sync(); | |
const selectedText = range.text; | |
// Concatenate the input box text, the label 'Input:', and the selected text | |
const fullPrompt = promptText + " Input: " + selectedText; | |
// Make the API request with the concatenated prompt | |
const apiResponse = await makeOpenAIRequest(fullPrompt); | |
// Insert the response with unique formatting | |
const responseRange = range.insertParagraph(apiResponse, Word.InsertLocation.after); | |
responseRange.font.color = "blue"; // Set the font color to blue | |
responseRange.font.italic = true; // Make the font italic | |
await context.sync(); | |
}); | |
} | |
async function makeOpenAIRequest(text: string): Promise<string> { | |
try { | |
const response = await fetch('https://api.openai.com/v1/chat/completions', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ` // Replace with your actual API key | |
}, | |
body: JSON.stringify({ | |
model: "gpt-3.5-turbo", | |
messages: [ | |
{ | |
role: "system", | |
content: "You are a personal assistant, adept at processing and responding to various queries and tasks." | |
}, | |
{ | |
role: "user", | |
content: text | |
} | |
] | |
}) | |
}); | |
const data = await response.json(); | |
return data.choices[0].message.content; // Modify this based on the response structure | |
} catch (error) { | |
console.error(error); | |
return `Error: ${error.message}`; | |
} | |
} | |
async function tryCatch(callback: () => Promise<void>) { | |
try { | |
await callback(); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
language: typescript | |
template: | |
content: |- | |
<input type="text" id="promptInput" placeholder="Enter your prompt here"> | |
<button id="run">Run</button> | |
language: html | |
style: | |
content: |- | |
section.samples { | |
margin-top: 20px; | |
} | |
section.samples .ms-Button, section.setup .ms-Button { | |
display: block; | |
margin-bottom: 5px; | |
margin-left: 20px; | |
min-width: 80px; | |
} | |
language: css | |
libraries: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
[email protected] | |
@types/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment