Skip to content

Commit

Permalink
Lotsa stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
thomax committed Apr 29, 2024
1 parent 380561b commit b50c38c
Show file tree
Hide file tree
Showing 29 changed files with 188 additions and 1 deletion.
44 changes: 44 additions & 0 deletions 2 - Interaktiv web/52 - Lokal LLM (chatbot)/chatbotIO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const apiUrl = 'http://127.0.0.1:11434/api/generate'

async function handlePromptInput() {
const inputText = document.getElementById('inputArea').value
document.getElementById('displayArea').innerHTML += `<p><span class="who">You:</span> <span class="text">${inputText}</span></p>`
console.log('Awaiting response...')
document.getElementById('inputArea').value = ''
const result = await createPrompt(inputText)
console.log('Got response:', result)
document.getElementById('displayArea').innerHTML += `<p><span class="who">Bot:</span> ${result.response}</span></p>`
}

async function createPrompt(message) {

const controller = new AbortController()
const timeoutId = setTimeout(() => {
return controller.abort()
}, 18000)

const requestData = {
model: 'phi3',
stream: false,
prompt: message
}

const requestOptions = {
method: 'POST', // request method (GET, POST, PUT, DELETE, etc.)
headers: {
'Content-Type': 'application/json' // type of content in request body
},
body: JSON.stringify(requestData), // convert requestData to JSON-format
signal: controller.signal
}

try {
return fetch(apiUrl, requestOptions).then((res) => res.json())
.then(data => data)
} catch (error) {
console.error(error)
return { response: error.message }
} finally {
clearTimeout(timeoutId)
}
}
32 changes: 32 additions & 0 deletions 2 - Interaktiv web/52 - Lokal LLM (chatbot)/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ollama chatbot frontend</title>
<script src="chatbotIO.js"></script>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 1.2rem;
}
p {
margin-bottom: 10px;
}
.who {
color: green;
font-weight: 700;
}
.text {
color: black;
}
</style>
</head>
<body>
<h2>Chat</h2>
<div id="displayArea"></div>
<textarea id="inputArea" rows="4" cols="50"></textarea>
<br />
<button onclick="handlePromptInput()">Submit</button>
</body>
</html>
69 changes: 69 additions & 0 deletions 2 - Interaktiv web/52 - Lokal LLM (chatbot)/oppgave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Hvordan sette opp en chatbot på din egen maksin

1. Last ned og staller Ollama: https://ollama.com/
a. Hvorfor heter dette "Ollama"??
2. Åpne ledetekst/cmd, og skriv ollama run tinyllama
3. Booom! Du har din egen chatbot!!

Funker? Vi skal nå kjøre Ollama som en server:

Lukk cmd-vindet, åpne et nytt. Skriv inn følgende, en linje av gangen.

NB: Du må også åpne et nytt cmd-vindu før du kaller `ollama serve`

## Windows 10

```
set OLLAMA_ORIGINS=*
set OLLAMA_HOST=127.0.0.1:11434
ollama serve
```

## Windows 11 (Powershell)

```
setx OLLAMA_ORIGINS *
setx OLLAMA_HOST 127.0.0.1:11434
ollama serve
```

Etter å ha gjort dette, må du åpne

Bruke Ollama-APIet, her finner du dokumentasjon: https://github.com/ollama/ollama/blob/main/docs/api.md

Koden under burde hjelpe deg i å komme i gang med API-et. Lag din egen KI-chat. Lykke til!

```js
const apiUrl = 'http://127.0.0.1:11434/api/generate'

function createPrompt(message) {
const requestData = {
model: 'tinyllama', // must exist on the server (your computer)
stream: false,
prompt: message
}

const requestOptions = {
method: 'POST', // request method (GET, POST, PUT, DELETE, etc.)
headers: {
'Content-Type': 'application/json' // type of content in request body
},
body: JSON.stringify(requestData), // convert requestData to JSON-format
signal: controller.signal
}
fetch(apiUrl, requestOptions)
.then((response) => {
console.log(response)
if (!response.ok) {
throw new Error('Network response was not ok')
}
return response.json() // convert response to JSON-format
})
.then((result) => {
console.log(result) // this is the actual result
})
.catch((error) => {
console.error('There was a problem with the fetch operation:', error)
})
}
```
Empty file.
Empty file.
Empty file modified 3 - Objektorientert programmering/monster-generator/.gitignore
100755 → 100644
Empty file.
Empty file.
Empty file modified 3 - Objektorientert programmering/monster-generator/README.md
100755 → 100644
Empty file.
Empty file modified 3 - Objektorientert programmering/monster-generator/index.html
100755 → 100644
Empty file.
Empty file.
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file modified 3 - Objektorientert programmering/monster-generator/src/app.css
100755 → 100644
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file modified 3 - Objektorientert programmering/monster-generator/src/main.js
100755 → 100644
Empty file.
Empty file.
Empty file.
Empty file.
44 changes: 43 additions & 1 deletion Eksamen/v23/10-b/game.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Troll extends GameObject {

increaseScore() {
this.score++
scoreElement.innerText = this.score
}

increseSpeed() {
Expand All @@ -64,10 +65,17 @@ class Obstacle extends GameObject {
}
}

function randomBetween(min, max) {
return Math.random() * (max - min) + min
}

function initializeGame() {
// spawn whatever
spawnTroll()
// spawn food
for (let n = 0; n < initialFoodCount; n++) {
spawnFood()
}
gameLoop()
}

Expand All @@ -83,6 +91,18 @@ function gameLoop() {
gameOverReason = 'You left the game area'
}
// check food collision
foods.forEach((food, index) => {
if (isColliding(food.rect(), troll.rect())) {
// goooood stemning
troll.increaseScore()
troll.increseSpeed(speedIncrement)
// remove food
food.element.remove()
foods.splice(index, 1)
// spawn obstacle
spawnObstacle(food.x, food.y)
}
})
// check obstacle collision
window.requestAnimationFrame(gameLoop)
}
Expand All @@ -93,6 +113,25 @@ function spawnTroll() {
troll = new Troll(gameWidth / 2, gameHeight / 2, trollInitialSpeed, element)
}

function spawnFood() {
const element = document.createElement('div')
element.classList.add('food')
gameAreaElement.appendChild(element)
const x = randomBetween(0, gameWidth - spriteSize)
const y = randomBetween(0, gameHeight - spriteSize)
food = new Food(x, y, element)
foods.push(food)
}

function spawnObstacle(x, y) {
const element = document.createElement('div')
element.classList.add('obstacle')
gameAreaElement.appendChild(element)
obstacle = new Obstacle(x, y, element)
obstacles.push(obstacle)
}


function isInside(innerRect, outerRect) {
return innerRect.x >= outerRect.x &&
innerRect.x + innerRect.width <= outerRect.x + outerRect.width &&
Expand Down Expand Up @@ -122,7 +161,7 @@ function onKeyDown(event) {
const spriteSize = 15
const trollInitialSpeed = 2
const speedIncrement = 1

const initialFoodCount = 3
const gameWidth = 1000
const gameHeight = 600
const gameAreaRect = { x: 0, y: 0, width: gameWidth, height: gameHeight }
Expand All @@ -131,6 +170,9 @@ const scoreElement = document.getElementById('score')
const messageElement = document.getElementById('message')
document.addEventListener('keydown', onKeyDown)

const foods = []
const obstacles = []

let gameOver = false
let troll
let gameOverReason
Expand Down
Empty file modified Eksamen/v23/10-b/game2.js
100755 → 100644
Empty file.
Empty file modified Eksamen/v23/10-b/index.html
100755 → 100644
Empty file.
Empty file modified Eksamen/v23/10-b/style.css
100755 → 100644
Empty file.
Empty file modified Eksamen/v23/3.js
100755 → 100644
Empty file.
Empty file modified Eksamen/v23/6.js
100755 → 100644
Empty file.
Empty file modified Eksamen/v23/9.js
100755 → 100644
Empty file.
Empty file modified Eksamen/v23/9a.js
100755 → 100644
Empty file.
Empty file modified Eksamen/v23/googleplaystore/googleplaystore.json
100755 → 100644
Empty file.

0 comments on commit b50c38c

Please sign in to comment.