continue icon indicating copy to clipboard operation
continue copied to clipboard

Added a function to docs how to discover all availble models from oll…

Open meganoob1337 opened this issue 2 years ago • 7 comments

…ama automatically

This is a function i wrote so the Config gets Prefilled with the Ollama Models that are present in the local installation, you don't need to accept the PR, but maybe you can add it somehow if you want. Here is my config.py:

"""
This is the Continue configuration file.

See https://continue.dev/docs/customization to for documentation of the available options.
"""
from continuedev.src.continuedev.libs.llm.ollama import Ollama

from continuedev.src.continuedev.core.models import Models
from continuedev.src.continuedev.core.config import CustomCommand, SlashCommand, ContinueConfig
from continuedev.src.continuedev.plugins.context_providers.github import GitHubIssuesContextProvider
from continuedev.src.continuedev.libs.llm.maybe_proxy_openai import MaybeProxyOpenAI

from continuedev.src.continuedev.plugins.steps.open_config import OpenConfigStep
from continuedev.src.continuedev.plugins.steps.clear_history import ClearHistoryStep
from continuedev.src.continuedev.plugins.steps.feedback import FeedbackStep
from continuedev.src.continuedev.plugins.steps.comment_code import CommentCodeStep
from continuedev.src.continuedev.plugins.steps.share_session import ShareSessionStep
from continuedev.src.continuedev.plugins.steps.main import EditHighlightedCodeStep
from continuedev.src.continuedev.plugins.steps.cmd import GenerateShellCommandStep
from continuedev.src.continuedev.plugins.context_providers.search import SearchContextProvider
from continuedev.src.continuedev.plugins.context_providers.diff import DiffContextProvider
from continuedev.src.continuedev.plugins.context_providers.url import URLContextProvider
from continuedev.src.continuedev.plugins.context_providers.terminal import TerminalContextProvider
import requests

def get_model_names(url):
    try:
        # Send a GET request to the URL
        response = requests.get(url)

        # Check if the request was successful (status code 200)
        if response.status_code == 200:
            # Parse the JSON data from the response
            data = response.json()

            # Extract the "name" field from each object in the "models" array
            names = [model["name"] for model in data.get("models", [])]

            return names
        else:
            # Print an error message if the request was not successful
            print(f"Failed to fetch data. Status code: {response.status_code}")
            return []
    except Exception as e:
        # Handle any exceptions that may occur during the request
        print(f"An error occurred: {str(e)}")
        return []

# Function to process model names
def process_model_name(name):
    return Ollama(
			context_length=2048,
			model=name,
			timeout=300,
			prompt_templates={'edit': 'Consider the following code:\n```\n{{{code_to_edit}}}\n```\nEdit the code to perfectly satisfy the following user request:\n{{{user_input}}}\nOutput nothing except for the code. No code block, no English explanation, no start/end tags.'},
			server_url="http://localhost:11434"
		)  # Example: Convert names to uppercase

# Example usage:
url = "http://localhost:11434/api/tags"
model_names = get_model_names(url)

# Check if model_names is empty
if not model_names:
    processed_names = []
else:
    # Use map to apply the process_model_name function to each name in the list
    processed_names = list(map(process_model_name, model_names))

config = ContinueConfig(
    allow_anonymous_telemetry=True,
    models=Models(
		unused=processed_names,
		default=Ollama(
			context_length=2048,
			model="codellama:13b",
			timeout=300,
			prompt_templates={'edit': 'Consider the following code:\n```\n{{{code_to_edit}}}\n```\nEdit the code to perfectly satisfy the following user request:\n{{{user_input}}}\nOutput nothing except for the code. No code block, no English explanation, no start/end tags.'},
			server_url="http://localhost:11434"
		)
	),
    system_message=None,
    temperature=0.5,
    custom_commands=[
        CustomCommand(
            name="test",
            description="Write unit tests for the highlighted code",
            prompt="Write a comprehensive set of unit tests for the selected code. It should setup, run tests that check for correctness including important edge cases, and teardown. Ensure that the tests are complete and sophisticated. Give the tests just as chat output, don't edit any file.",
        )
    ],
    slash_commands=[
        SlashCommand(
            name="edit",
            description="Edit code in the current file or the highlighted code",
            step=EditHighlightedCodeStep,
        ),
        SlashCommand(
            name="config",
            description="Customize Continue - slash commands, LLMs, system message, etc.",
            step=OpenConfigStep,
        ),
        SlashCommand(
            name="comment",
            description="Write comments for the current file or highlighted code",
            step=CommentCodeStep,
        ),
        SlashCommand(
            name="feedback",
            description="Send feedback to improve Continue",
            step=FeedbackStep,
        ),
        SlashCommand(
            name="clear",
            description="Clear step history",
            step=ClearHistoryStep,
        ),
        SlashCommand(
            name="share",
            description="Download and share the session transcript",
            step=ShareSessionStep,
        ),
        SlashCommand(
            name="cmd",
            description="Generate a shell command",
            step=GenerateShellCommandStep,
        ),
    ],
    context_providers=[
        # GitHubIssuesContextProvider(
        #     repo_name="<your github username or organization>/<your repo name>",
        #     auth_token="<your github auth token>"
        # ),
        SearchContextProvider(),
        DiffContextProvider(),
        URLContextProvider(
            preset_urls = [
                # Add any common urls you reference here so they appear in autocomplete
            ]
        ),
        TerminalContextProvider(),
    ],
)

meganoob1337 avatar Sep 23 '23 12:09 meganoob1337

Deploy Preview for continuedev ready!

Name Link
Latest commit bb7cfae25793bfcda07f085fb2a509b13470d043
Latest deploy log https://app.netlify.com/sites/continuedev/deploys/650ed5547f8091000820a544
Deploy Preview https://deploy-preview-502--continuedev.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Sep 23 '23 12:09 netlify[bot]

This is awesome! It's nice to see something that couldn't be done if the config file was just .json. I'd actually love to include this in some sort of community plugin collection that we could share in the documentation—so I might refactor the way that this exists in the codebase, but will definitely merge!

sestinj avatar Sep 23 '23 18:09 sestinj

Yeah, I wasn't sure how to integrate it so most people can profit from it, and I don't have the insight into the project to know how to integrate it smartly , but I thought you might benefit from it/ know where to put it , because as a feature it would be really cool ;) so I wanted to share!

meganoob1337 avatar Sep 23 '23 18:09 meganoob1337

Totally! I actually think I'll use this myself, will make testing different models much nicer : )

sestinj avatar Sep 23 '23 20:09 sestinj

Yay impact 🤣 thanks for your work!

meganoob1337 avatar Sep 23 '23 21:09 meganoob1337

@meganoob1337 we started by posting in the #show-and-tell channel in Discord about this, will probably make this the focal point of community plugins until we get enough to warrant a docs page

And I'm doing a bunch of work on cleaning the config file experience (to get strong typing is one thing), and at that point I'll probably try and get this in the codebase

sestinj avatar Oct 05 '23 02:10 sestinj

Wow this is amazing @meganoob1337

ivanfioravanti avatar Oct 11 '23 18:10 ivanfioravanti

@meganoob1337 just cleaning up old PRs. Even though we're no longer using Python at all, I hope you know that this contribution inspired our "AUTODETECT" mode, which is now built into Continue! So thank you for your work : )

sestinj avatar Jun 27 '24 19:06 sestinj