Skip to content
This repository was archived by the owner on Jun 2, 2026. It is now read-only.

Added Ollama integration#22

Merged
andrewpareles merged 17 commits into
voideditor:mainfrom
w1gs:main
Oct 2, 2024
Merged

Added Ollama integration#22
andrewpareles merged 17 commits into
voideditor:mainfrom
w1gs:main

Conversation

@w1gs

@w1gs w1gs commented Sep 18, 2024

Copy link
Copy Markdown
Contributor

This PR adds in the Ollama integration. Two new settings were added for Ollama (endpoint and model). Instead of using the Ollama node library a fetch request directly to the provided endpoint is used. A local instance of the Ollama API can be started with the command ollama serve. The OLLAMA_ORIGINS=* environment variable needs to be set to allow the extension to make requests to Ollama.

@andrewpareles

Copy link
Copy Markdown
Contributor

Nice! Will test this in a bit. It would be great to add setup instructions for this, just added #26.

@w1gs

w1gs commented Sep 18, 2024

Copy link
Copy Markdown
Contributor Author

Nice! Will test this in a bit. It would be great to add setup instructions for this, just added #26.

Awesome. Where would be the best place to add the instructions to set that up?

@mathewpareles

mathewpareles commented Sep 19, 2024

Copy link
Copy Markdown
Contributor

It should probably appear in a new window when VS Code starts up, similar to the "Welcome" page. We're also open to alternatives.
image

@okxiaoliang4 okxiaoliang4 mentioned this pull request Sep 20, 2024
5 tasks

@BruceMacD BruceMacD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this, I was just trying a similar approach myself.

Some notes for anyone that is looking to try this:

  • Ollama doesn't allow CORs requests from VSCode extensions by default. So you'll have to stop any running Ollama server and manually set it to allow the extension origin, as w1gs documented.
    OLLAMA_ORIGINS="vscode-webview://*" ollama serve

Comment thread extensions/void/src/common/sendLLMMessage.ts Outdated
Comment thread extensions/void/package.json Outdated
Co-authored-by: Bruce MacDonald <brucewmacdonald@gmail.com>
@andrewpareles

andrewpareles commented Sep 21, 2024

Copy link
Copy Markdown
Contributor
  1. @BruceMacD Great call. @w1gs, we should definitely use ollama/browser instead of calling fetch(). This should be easy to swap out using Bruce's code. My only change would be to make it use .try().catch() to go with the convention of the rest of the file.
import ollama from 'ollama/browser';

const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({ messages, onText, onFinalMessage, apiConfig }) => {
	const ollama = new Ollama({ host: apiConfig.ollama.host });

	let did_abort = false
	let fullText = ''

	// if abort is called, onFinalMessage is NOT called, and no later onTexts are called either
	let abort: () => void = () => { did_abort = true }

	ollama.chat({ model: apiConfig.ollama.model, messages: messages, stream: true })
		.then(async response => {

			abort = () => {
				ollama.abort();
				did_abort = true;
			}

			try {
				for await (const part of response) {
					if (did_abort) return
					let newText = part.message.content
					fullText += newText
					onText(newText, fullText)
				}
			}
			// when error/fail
			catch (e) {
				onFinalMessage(fullText)
				return
			}

			// when we get the final message on this stream
			onFinalMessage(fullText)
		})

	return { abort };
}
  1. I'm not sure if there's a way to specify the Ollama endpoint in ollama/browser. If there's not, I'd be fine getting rid of the apiConfig.ollama.model variable and assuming users want to host on Ollama's default url.

@w1gs

w1gs commented Sep 21, 2024

Copy link
Copy Markdown
Contributor Author

@andrewpareles I was able to get it setup with ollama/browser and it works great. I also made some changes that will detect if there aren't any API keys set, and set the extension to disabled. When disabled it will be blurred out and not interactable. If you try to send cmd+l when no keys are set, a warning box will appear. As the configuration updates so does the extension. I do agree that the Ollama setup instructions should be somewhere but I figured a simple enable/disable mechanism would be good for now.

Disabled extension:
CleanShot 2024-09-21 at 07 01 11@2x

Warning when trying to add text when no keys are set:
CleanShot 2024-09-21 at 07 02 18@2x

Extension enabled:
CleanShot 2024-09-21 at 07 03 21@2x

@andrewpareles

Copy link
Copy Markdown
Contributor

I'll review this in a bit!

@andrewpareles

Copy link
Copy Markdown
Contributor

Before closing this, see #74

@w1gs

w1gs commented Oct 1, 2024

Copy link
Copy Markdown
Contributor Author

Fixed the merge conflicts.

@andrewpareles andrewpareles merged commit 653d5e9 into voideditor:main Oct 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants