Skip to content

Commit

Permalink
Merge branch 'refs/heads/codegeneration'
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmurdza committed Dec 17, 2023
2 parents ba1d119 + 3f04751 commit e93155b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
49 changes: 17 additions & 32 deletions generate.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
import re
import os
from openai import OpenAI
from codechain.generation import ModifyCodeChain
from langchain.chat_models import ChatOpenAI

# Function to replace bash code blocks with python code blocks
def replace_bash_with_python(match):
bash_code = match.group(1)

# Initialize the OpenAI client
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY")
)

# Call OpenAI API to translate Bash to Python
prompt = f'''\
{bash_code}
bash_to_python_prompt = '''\
convert this block of code as concisely as possible to
```python
import requests
import os
response = requests.post(
"https://...",
headers={
headers={{
...
},
}},
...
)
```
use os.environ to get API keys
don't use intermediate variables
always use code fences ```python and ``` around code blocks in the output
always use double quotes
'''
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": prompt
}
],
model="gpt-3.5-turbo"
)

python_code = response.choices[0].message.content.strip()
return python_code
# Function to replace bash code blocks with python code blocks
def replace_bash_with_python(match):
bash_code = match.group(1)

# Call OpenAI API to translate Bash to Python
generator = ModifyCodeChain.from_instruction(
bash_to_python_prompt,
ChatOpenAI(model="gpt-3.5-turbo", temperature=0.2)
)
result = '```python\n' + generator.run(bash_code) + '\n```'
return result

# Read the content of README.md
with open("README.md", "r") as readme_file:
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
regex
openai
openai
langchain
codechain

0 comments on commit e93155b

Please sign in to comment.