-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core…
… into gh-pages
- Loading branch information
Showing
20 changed files
with
6,842 additions
and
3,403 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<script> | ||
|
||
|
||
class Foo { | ||
|
||
async init() { | ||
|
||
} | ||
|
||
setupListeners() { | ||
this.s | ||
} | ||
|
||
setupFoo() { | ||
|
||
} | ||
} | ||
|
||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,6 @@ import {AudioRecorder} from "src/client/audio.js" | |
<script> | ||
|
||
|
||
|
||
var foo = <span>x</span>, b = 3; | ||
|
||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# AI Function Calling | ||
|
||
<script> | ||
import OpenAI from "demos/openai/openai.js" | ||
|
||
let maxCalls = 3 | ||
let calls = 0 | ||
|
||
function getCurrentDateTime() { | ||
return new Date().toLocaleString(); | ||
} | ||
|
||
const functions = { | ||
getCurrentDateTime: getCurrentDateTime, | ||
}; | ||
|
||
async function handleFunctionCall(functionName, args) { | ||
if (functions[functionName]) { | ||
return functions[functionName](...args); | ||
} else { | ||
throw new Error(`Function ${functionName} not found`); | ||
} | ||
} | ||
|
||
let messages = [ | ||
{ "role": "system", "content": "You are an AI chat bot!" }, | ||
{ "role": "user", "content": [ | ||
{ | ||
"type": "text", | ||
"text": "What is the current time?" | ||
} | ||
]} | ||
] | ||
|
||
|
||
async function chat() { | ||
calls++ | ||
if (calls > maxCalls) { | ||
lively.warn("max calls reached") | ||
return | ||
} | ||
|
||
let prompt = { | ||
"model": "gpt-4o", | ||
"max_tokens": 2000, | ||
"temperature": 0.1, | ||
"top_p": 1, | ||
"n": 1, | ||
"stream": false, | ||
"stop": "VANILLA", | ||
"messages": messages, | ||
"functions": [ | ||
{ | ||
"name": "getCurrentDateTime", | ||
"description": "Fetches the current date and time", | ||
|
||
}, | ||
{ | ||
name: "addNumbers", | ||
description: "Adds two numbers", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
a: { type: "number" }, | ||
b: { type: "number" } | ||
}, | ||
required: ["a", "b"] | ||
} | ||
} | ||
] | ||
} | ||
|
||
let json = await OpenAI.openAIRequest(prompt).then(r => r.json()) | ||
if (!json.choices) { | ||
result.textContent += JSON.stringify(json, undefined, 2) | ||
return | ||
} | ||
|
||
|
||
let message = json.choices[0].message | ||
messages.push(message) | ||
|
||
result.textContent = JSON.stringify(messages, undefined, 2) | ||
|
||
if (message.function_call) { | ||
const functionName = message.function_call.name; | ||
const args = message.function_call.arguments || []; | ||
const result = await handleFunctionCall(functionName, args); | ||
|
||
|
||
messages.push({ | ||
role: 'function', | ||
name: functionName, | ||
content: JSON.stringify(result) | ||
}) | ||
chat() | ||
} | ||
} | ||
|
||
let result = <div style="white-space: pre-wrap"></div> | ||
|
||
let pane = <div> | ||
<button click={() => chat()}>chat</button> | ||
{result} | ||
</div> | ||
pane | ||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# AI Streaming Chat | ||
|
||
<script> | ||
import OpenAI from "demos/openai/openai.js" | ||
|
||
let messages = [ | ||
{ "role": "system", "content": "You are an AI chat bot!" }, | ||
{ "role": "user", "content": [ | ||
{ | ||
"type": "text", | ||
"text": "Tell me a funny story about walking and talking trees!" | ||
} | ||
]} | ||
] | ||
|
||
async function chat() { | ||
|
||
let prompt = { | ||
"model": "gpt-4o", | ||
"max_tokens": 2000, | ||
"temperature": 0.1, | ||
"top_p": 1, | ||
"n": 1, | ||
"stream": true, | ||
"stop": "VANILLA", | ||
"messages": messages | ||
} | ||
|
||
|
||
let response = await OpenAI.openAIRequest(prompt) | ||
const reader = response.body.getReader(); | ||
const decoder = new TextDecoder('utf-8'); | ||
const chunks = [] | ||
|
||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
|
||
const chunk = decoder.decode(value, { stream: true }); | ||
|
||
// the format seems to be Server-Sent Events (SSE) | ||
let jsonSources = chunk | ||
.split("\n\n") | ||
.filter(ea => ea) | ||
.map(ea => ea.replace(/^data: /,"")) | ||
|
||
try { | ||
for(let source of jsonSources) { | ||
if (source == "[DONE]") { | ||
// result.textContent += "DONE!!!" | ||
} else { | ||
const json = JSON.parse(source) | ||
|
||
chunks.push(json) | ||
|
||
// result.textContent += JSON.stringify(json.choices[0].delta) + "\n" | ||
|
||
const content = json.choices[0].delta.content | ||
if (content) { | ||
result.textContent += content | ||
} | ||
} | ||
|
||
} | ||
} catch(e) { | ||
result.textContent += "\nERROR: " + e + " while parsing: '" + chunk + "'" | ||
} | ||
} | ||
} | ||
|
||
let result = <div style="white-space: pre-wrap"></div> | ||
|
||
let pane = <div> | ||
<button click={() => chat()}>chat</button> | ||
{result} | ||
</div> | ||
pane | ||
</script> |
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
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
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
Oops, something went wrong.