config cleaning

This commit is contained in:
Nate Sesti 2023-07-24 22:53:10 -07:00
parent e788f27ae9
commit a993b2ff47
8 changed files with 209 additions and 4 deletions

View file

@ -18,6 +18,7 @@ from .main import Context, ContinueCustomException, History, HistoryNode, Step,
from ..plugins.steps.core.core import *
from ..libs.llm.proxy_server import ProxyServer
from ..libs.util.telemetry import posthog_logger
from ..libs.util.paths import getConfigFilePath
class Autopilot:
@ -258,7 +259,7 @@ class ContinueSDK(AbstractContinueSDK):
def _load_config_dot_py(self) -> ContinueConfig:
# Use importlib to load the config file config.py at the given path
path = os.path.join(os.path.expanduser("~"), ".continue", "config.py")
path = getConfigFilePath()
try:
import importlib.util
spec = importlib.util.spec_from_file_location("config", path)

View file

@ -0,0 +1,87 @@
"""
This is the Continue configuration file.
If you aren't getting strong typing on these imports,
be sure to select the Python interpreter in ~/.continue/server/env.
"""
import subprocess
from continuedev.src.continuedev.core.main import Step
from continuedev.src.continuedev.core.sdk import ContinueSDK
from continuedev.src.continuedev.core.config import CustomCommand, SlashCommand, ContinueConfig
from continuedev.src.continuedev.plugins.context_providers.github import GitHubIssuesContextProvider
from continuedev.src.continuedev.plugins.context_providers.google import GoogleContextProvider
class CommitMessageStep(Step):
"""
This is a Step, the building block of Continue.
It can be used below as a slash command, so that
run will be called when you type '/commit'.
"""
async def run(self, sdk: ContinueSDK):
# Get the root directory of the workspace
dir = sdk.ide.workspace_directory
# Run git diff in that directory
diff = subprocess.check_output(
["git", "diff"], cwd=dir).decode("utf-8")
# Ask gpt-3.5-16k to write a commit message,
# and set it as the description of this step
self.description = await sdk.models.gpt3516k.complete(
f"{diff}\n\nWrite a short, specific (less than 50 chars) commit message about the above changes:")
config = ContinueConfig(
# If set to False, we will not collect any usage data
# See here to learn what anonymous data we collect: https://continue.dev/docs/telemetry
allow_anonymous_telemetry=True,
# GPT-4 is recommended for best results
# See options here: https://continue.dev/docs/customization#change-the-default-llm
default_model="gpt-4",
# Set a system message with information that the LLM should always keep in mind
# E.g. "Please give concise answers. Always respond in Spanish."
system_message=None,
# Set temperature to any value between 0 and 1. Higher values will make the LLM
# more creative, while lower values will make it more predictable.
temperature=0.5,
# Custom commands let you map a prompt to a shortened slash command
# They are like slash commands, but more easily defined - write just a prompt instead of a Step class
# Their output will always be in chat form
custom_commands=[CustomCommand(
name="test",
description="This is an example custom command. Use /config to edit it and create more",
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 let you run a Step from a slash command
slash_commands=[
# SlashCommand(
# name="commit",
# description="This is an example slash command. Use /config to edit it and create more",
# step=CommitMessageStep,
# )
],
# Context providers let you quickly select context by typing '@'
# Uncomment the following to
# - quickly reference GitHub issues
# - show Google search results to the LLM
context_providers=[
# GitHubIssuesContextProvider(
# repo_name="<your github username or organization>/<your repo name>",
# auth_token="<your github auth token>"
# ),
# GoogleContextProvider(
# serper_api_key="<your serper.dev api key>"
# )
]
)

View file

@ -25,3 +25,22 @@ def getSessionFilePath(session_id: str):
path = os.path.join(getSessionsFolderPath(), f"{session_id}.json")
os.makedirs(os.path.dirname(path), exist_ok=True)
return path
def getDefaultConfigFile() -> str:
current_path = os.path.dirname(os.path.realpath(__file__))
config_path = os.path.join(
current_path, "..", "constants", "default_config.py.txt")
with open(config_path, 'r') as f:
return f.read()
def getConfigFilePath() -> str:
path = os.path.join(getGlobalFolderPath(), "config.py")
os.makedirs(os.path.dirname(path), exist_ok=True)
if not os.path.exists(path):
with open(path, 'w') as f:
f.write(getDefaultConfigFile())
return path

View file

@ -0,0 +1,35 @@
from typing import List
from github import Github
from github import Auth
from ...core.context import ContextProvider, ContextItemDescription, ContextItem, ContextItemId
class GitHubIssuesContextProvider(ContextProvider):
"""
The GitHubIssuesContextProvider is a ContextProvider
that allows you to search GitHub issues in a repo.
"""
title = "issues"
repo_name: str
auth_token: str
async def provide_context_items(self) -> List[ContextItem]:
auth = Auth.Token(self.auth_token)
gh = Github(auth=auth)
repo = gh.get_repo(self.repo_name)
issues = repo.get_issues().get_page(0)
return [ContextItem(
content=issue.body,
description=ContextItemDescription(
name=f"Issue #{issue.number}",
description=issue.title,
id=ContextItemId(
provider_title=self.title,
item_id=issue.id
)
)
) for issue in issues]

View file

@ -0,0 +1,64 @@
import json
from typing import List
import aiohttp
from ...core.main import ContextItem, ContextItemDescription, ContextItemId
from ...core.context import ContextProvider
class GoogleContextProvider(ContextProvider):
title = "google"
serper_api_key: str
GOOGLE_CONTEXT_ITEM_ID = "google_search"
@property
def BASE_CONTEXT_ITEM(self):
return ContextItem(
content="",
description=ContextItemDescription(
name="Google Search",
description="Enter a query to search google",
id=ContextItemId(
provider_title=self.title,
item_id=self.GOOGLE_CONTEXT_ITEM_ID
)
)
)
async def _google_search(self, query: str) -> str:
url = "https://google.serper.dev/search"
payload = json.dumps({
"q": query
})
headers = {
'X-API-KEY': self.serper_api_key,
'Content-Type': 'application/json'
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=payload) as response:
return await response.text()
async def provide_context_items(self) -> List[ContextItem]:
return [self.BASE_CONTEXT_ITEM]
async def get_item(self, id: ContextItemId, query: str, _) -> ContextItem:
if not id.item_id == self.GOOGLE_CONTEXT_ITEM_ID:
raise Exception("Invalid item id")
results = await self._google_search(query)
json_results = json.loads(results)
content = f"Google Search: {query}\n\n"
if answerBox := json_results.get("answerBox"):
content += f"Answer Box ({answerBox['title']}): {answerBox['answer']}\n\n"
for result in json_results["organic"]:
content += f"{result['title']}\n{result['link']}\n{result['snippet']}\n\n"
ctx_item = self.BASE_CONTEXT_ITEM.copy()
ctx_item.content = content
ctx_item.description.id.item_id = query
return ctx_item

View file

@ -1,6 +1,7 @@
from textwrap import dedent
from ...core.main import Step
from ...core.sdk import ContinueSDK
from ...libs.util.paths import getConfigFilePath
import os
@ -25,6 +26,4 @@ class OpenConfigStep(Step):
`prompt` is the instruction given to the model. The overall prompt becomes "Task: {prompt}, Additional info: {user_input}". For example, if you entered "/test exactly 5 assertions", the overall prompt would become "Task: Write a comprehensive...and sophisticated, Additional info: exactly 5 assertions".""")
async def run(self, sdk: ContinueSDK):
global_dir = os.path.expanduser('~/.continue')
config_path = os.path.join(global_dir, 'config.py')
await sdk.ide.setFileOpen(config_path)
await sdk.ide.setFileOpen(getConfigFilePath())