Skip to content

Commit

Permalink
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core
Browse files Browse the repository at this point in the history
… into gh-pages
  • Loading branch information
onsetsu committed Jun 5, 2024
2 parents 3e1a32e + 7d407a4 commit e8bc8ce
Show file tree
Hide file tree
Showing 20 changed files with 6,842 additions and 3,403 deletions.
28 changes: 28 additions & 0 deletions demos/markdown/scripts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@









<script>


class Foo {

async init() {

}

setupListeners() {
this.s
}

setupFoo() {

}
}

</script>
2 changes: 1 addition & 1 deletion demos/markdown/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import {AudioRecorder} from "src/client/audio.js"
<script>



var foo = <span>x</span>, b = 3;

</script>
107 changes: 107 additions & 0 deletions demos/openai/functions.md
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>
78 changes: 78 additions & 0 deletions demos/openai/streaming.md
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>
11 changes: 10 additions & 1 deletion src/components/tools/openai-audio-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export default class OpenaiAudioChat extends Morph {
// }
]
}

// remember voice and model in preference
this.voiceBox.value = lively.preferences.get("openai-audio-chat-voice") || this.voiceBox.value
this.voiceBox.addEventListener("change",
() => lively.preferences.set("openai-audio-chat-voice", this.voiceBox.value))
this.modelBox.value = lively.preferences.get("openai-audio-chat-model") || this.modelBox.value
this.modelBox.addEventListener("change",
() => lively.preferences.set("openai-audio-chat-model", this.modelBox.value))

this.setupUI()
await this.renderConversation()
lively.ensureID(this)
Expand Down Expand Up @@ -154,7 +163,7 @@ export default class OpenaiAudioChat extends Morph {

let prompt = {
"model": this.modelBox.value,
"max_tokens": 500,
"max_tokens": 2000,
"temperature": 0.1,
"top_p": 1,
"n": 1,
Expand Down
27 changes: 25 additions & 2 deletions src/components/widgets/ast-capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -1480,14 +1480,37 @@ ${lineContent}
}

/*MD ## Navigation MD*/
fixSourceCode(sourceCode) {
// replace all none code parts of a markdown or HTML file with whitespace, but keep line numbers stable...
if (this.lcm.mode == "gfm" || this.lcm.mode == "text/html") {
let cleanSource = ""
var blocks = sourceCode.split(/(?<=\<script[^>]*?>)|(?=\<\/script>)/gi);
for (var j = 1; j < blocks.length; j += 2) {
let prefix = blocks[j - 1].split("").map(ea => ea == "\n" ? "\n" : " ").join("")
let code = blocks[j]

cleanSource += prefix
cleanSource += code
}
return cleanSource

} else {
return sourceCode
}
}


/**
* Get the root path
*/
get programPath() {
if (!this.myProgramPath && !this.parsingFailed) {
// #TODO #Idea to get AST navigation in scripts in markdown, we could overwrite all non script content with whitespace, because we case for correct character positions
this.myProgramPath = this.programPathFor(this.sourceCode);

this.myProgramPath = this.programPathFor(this.fixSourceCode(this.sourceCode));
this.parsingFailed = !this.myProgramPath;
if (this.parsingFailed) {
lively.warn("ast-capabilities parsingFailed")
}
}
return this.myProgramPath;
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/widgets/lively-code-mirror-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ CodeMirror.registerHelper("lint", "markdown", function(text, options) {

function processJS(text, options, found) {

var blocks = text.split(/<script[\s\S]*?>|<\/script>/gi);

var blocks = text.split(/(?<=\<script[^>]*?>)|(?=\<\/script>)/gi);
debugger
let fullCode = ""
let offsets = [{line: -1, offset: 0}]

Expand Down
Loading

0 comments on commit e8bc8ce

Please sign in to comment.