Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Patch File Generation to AI Bot #4

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# build output
dist/
# generated types
.astro/

# dependencies
node_modules/

# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*


# environment variables
.env
.env.production

# macOS-specific files
.DS_Store
4 changes: 4 additions & 0 deletions docs/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"recommendations": ["astro-build.astro-vscode"],
"unwantedRecommendations": []
}
11 changes: 11 additions & 0 deletions docs/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "./node_modules/.bin/astro dev",
"name": "Development server",
"request": "launch",
"type": "node-terminal"
}
]
}
53 changes: 53 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Starlight Starter Kit: Basics

[![Built with Starlight](https://astro.badg.es/v2/built-with-starlight/tiny.svg)](https://starlight.astro.build)

```
npm create astro@latest -- --template starlight
```

[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/starlight/tree/main/examples/basics)
[![Open with CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/withastro/starlight/tree/main/examples/basics)

> 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun!

## 🚀 Project Structure

Inside of your Astro + Starlight project, you'll see the following folders and files:

```
.
├── public/
├── src/
│ ├── assets/
│ ├── content/
│ │ ├── docs/
│ │ └── config.ts
│ └── env.d.ts
├── astro.config.mjs
├── package.json
└── tsconfig.json
```

Starlight looks for `.md` or `.mdx` files in the `src/content/docs/` directory. Each file is exposed as a route based on its file name.

Images can be added to `src/assets/` and embedded in Markdown with a relative link.

Static assets, like favicons, can be placed in the `public/` directory.

## 🧞 Commands

All commands are run from the root of the project, from a terminal:

| Command | Action |
| :------------------------ | :----------------------------------------------- |
| `npm install` | Installs dependencies |
| `npm run dev` | Starts local dev server at `localhost:4321` |
| `npm run build` | Build your production site to `./dist/` |
| `npm run preview` | Preview your build locally, before deploying |
| `npm run astro ...` | Run CLI commands like `astro add`, `astro check` |
| `npm run astro -- --help` | Get help using the Astro CLI |

## 👀 Want to learn more?

Check out [Starlight’s docs](https://starlight.astro.build/), read [the Astro documentation](https://docs.astro.build), or jump into the [Astro Discord server](https://astro.build/chat).
27 changes: 27 additions & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';

// https://astro.build/config
export default defineConfig({
integrations: [
starlight({
title: 'My Docs',
social: {
github: 'https://github.com/withastro/starlight',
},
sidebar: [
{
label: 'Guides',
items: [
// Each item here is one entry in the navigation menu.
{ label: 'Example Guide', link: '/guides/example/' },
],
},
{
label: 'Reference',
autogenerate: { directory: 'reference' },
},
],
}),
],
});
17 changes: 17 additions & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "docs",
"type": "module",
"version": "0.0.1",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
},
"dependencies": {
"@astrojs/starlight": "^0.11.0",
"astro": "^3.2.3",
"sharp": "^0.32.5"
}
}
1 change: 1 addition & 0 deletions docs/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/src/assets/houston.webp
Binary file not shown.
7 changes: 7 additions & 0 deletions docs/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineCollection } from 'astro:content';
import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';

export const collections = {
docs: defineCollection({ schema: docsSchema() }),
i18n: defineCollection({ type: 'data', schema: i18nSchema() }),
};
11 changes: 11 additions & 0 deletions docs/src/content/docs/guides/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Example Guide
description: A guide in my new Starlight docs site.
---

Guides lead a user through a specific task they want to accomplish, often with a sequence of steps.
Writing a good guide requires thinking about what your users are trying to do.

## Further reading

- Read [about how-to guides](https://diataxis.fr/how-to-guides/) in the Diátaxis framework
36 changes: 36 additions & 0 deletions docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: Welcome to Starlight
description: Get started building your docs site with Starlight.
template: splash
hero:
tagline: Congrats on setting up a new Starlight project!
image:
file: ../../assets/houston.webp
actions:
- text: Example Guide
link: /guides/example/
icon: right-arrow
variant: primary
- text: Read the Starlight docs
link: https://starlight.astro.build
icon: external
---

import { Card, CardGrid } from '@astrojs/starlight/components';

## Next steps

<CardGrid stagger>
<Card title="Update content" icon="pencil">
Edit `src/content/docs/index.mdx` to see this page change.
</Card>
<Card title="Add new content" icon="add-document">
Add Markdown or MDX files to `src/content/docs` to create new pages.
</Card>
<Card title="Configure your site" icon="setting">
Edit your `sidebar` and other config in `astro.config.mjs`.
</Card>
<Card title="Read the docs" icon="open-book">
Learn more in [the Starlight Docs](https://starlight.astro.build/).
</Card>
</CardGrid>
11 changes: 11 additions & 0 deletions docs/src/content/docs/reference/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Example Reference
description: A reference page in my new Starlight docs site.
---

Reference pages are ideal for outlining how things work in terse and clear terms.
Less concerned with telling a story or addressing a specific use case, they should give a comprehensive outline of what your documenting.

## Further reading

- Read [about reference](https://diataxis.fr/reference/) in the Diátaxis framework
2 changes: 2 additions & 0 deletions docs/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />
3 changes: 3 additions & 0 deletions docs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "astro/tsconfigs/strict"
}
35 changes: 19 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

openai.api_key = OPENAI_API_KEY


# Initialize logging
logging.basicConfig(level=logging.INFO)

Expand Down Expand Up @@ -184,17 +183,22 @@ def solve_problem(mention, issue_number, issue_description):

prompt = generate_gpt_prompt(issue_data)

out = run_query(
ai_response, patch_file = run_query(
prompt,
mention["repository"]["owner"]["login"],
mention["repository"]["name"],
)

# response = generate_response(issue_description)
# print(response)
post_comment_to_issue(
issue_number,
out,
ai_response,
mention["repository"]["owner"]["login"],
mention["repository"]["name"],
)

post_comment_to_issue(
issue_number,
patch_file,
mention["repository"]["owner"]["login"],
mention["repository"]["name"],
)
Expand Down Expand Up @@ -242,16 +246,16 @@ def respond_to_unread_issues():
post_comment_to_issue(
issue_number=issue_number,
comment_text=f"""#### 🛑 **Rate Limit Exceeded!**

⌛ **Limit:** {USAGE_LIMIT} requests / day / repository
🔒 **Refreshes In:** {int(hours)} hours, {int(minutes)} minutes

<!-- To continue using the service, please consider upgrading to our **Pro Plan**.

##### 🚀 **Upgrade to Pro**
Upgrade to the Pro Plan to enjoy enhanced access, faster response times, and priority support. Click [here](Upgrade_Link) to upgrade now! -->

📬 For any inquiries for support or rate limit extension, please contact <a href="https://discord.gg/T6Hz6zpK7D" target="_blank">Support</a>.""",
⌛ **Limit:** {USAGE_LIMIT} requests / day / repository
🔒 **Refreshes In:** {int(hours)} hours, {int(minutes)} minutes
<!-- To continue using the service, please consider upgrading to our **Pro Plan**.
##### 🚀 **Upgrade to Pro**
Upgrade to the Pro Plan to enjoy enhanced access, faster response times, and priority support. Click [here](Upgrade_Link) to upgrade now! -->
📬 For any inquiries for support or rate limit extension, please contact <a href="https://discord.gg/T6Hz6zpK7D" target="_blank">Support</a>.""",
OWNER=mention["repository"]["owner"]["login"],
REPO=mention["repository"]["name"],
)
Expand Down Expand Up @@ -287,7 +291,6 @@ def respond_to_unread_issues():
# response = generate_response(problem_description)

# # Post the response as a comment on the issue
# post_comment_to_issue(issue_number, response)

# return jsonify({"message": "Webhook processed successfully"})

Expand Down
65 changes: 65 additions & 0 deletions utils/github_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ def setup_repo(owner, repo_name):
print("Repo already exists")


import difflib

def run_query(query: str, owner: str, repo_name: str):
setup_repo(owner, repo_name)
matched_docs = vector_store.similarity_search(
Expand All @@ -284,6 +286,69 @@ def run_query(query: str, owner: str, repo_name: str):

current_tokens = 0

for doc in matched_docs:
doc_content = doc.page_content + "\n\n"
doc_tokens = num_tokens_from_string(doc_content)
print(matched_docs.index(doc), doc_tokens)
if current_tokens + doc_tokens < MAX_TOKENS:
code_str += doc_content
current_tokens += doc_tokens
else:
break # stop adding more content if it exceeds the max token limit

template = """
You are Codebase AI. You are a super intelligent AI that answers questions about code bases.

You are:
- helpful & friendly
- good at answering complex questions in simple language
- an expert in all programming languages
- able to infer the intent of the user's question

The user will ask a question about their codebase, and you will answer it.

When the user asks their question, you will answer it by searching the codebase for the answer.

Here is the user's question and code file(s) you found to answer the question:

Question:
{query}

Code file(s):
{code}

[END OF CODE FILE(S)]w

Now answer the question using the code file(s) above.
"""

chat = ChatOpenAI(
streaming=True,
callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
verbose=True,
temperature=0.5,
)
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt])
chain = LLMChain(llm=chat, prompt=chat_prompt)

print("running chain...")
ai_said = chain.run(code=code_str, query=query)
print("chain output...")

# Generate patch file
original_code = code_str.splitlines()
modified_code = code_str.replace(query, ai_said).splitlines()
diff = difflib.unified_diff(original_code, modified_code)
patch_file = '\n'.join(diff)

return ai_said, patch_file

code_str = ""
MAX_TOKENS = 3500

current_tokens = 0

for doc in matched_docs:
doc_content = doc.page_content + "\n\n"
doc_tokens = num_tokens_from_string(doc_content)
Expand Down