Skip to content

Merge new SDK changes #46

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

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a256bec
Added autogen
calebcapoccia Oct 24, 2024
c65dcbf
Small change
calebcapoccia Oct 24, 2024
b5da699
Create update_sdk.yaml
calebcapoccia Oct 24, 2024
0b24e09
Update update_sdk.yaml
calebcapoccia Oct 24, 2024
31e7182
testing
calebcapoccia Oct 24, 2024
3cf59b8
Issues
calebcapoccia Oct 24, 2024
c44b397
Issues
calebcapoccia Oct 24, 2024
a4cb346
Requirements.txt
calebcapoccia Oct 24, 2024
9539503
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
f5d614a
More recent python openai
calebcapoccia Oct 25, 2024
e71909a
Merge branch 'master' of https://github.com/calebcapoccia/exa-py
calebcapoccia Oct 25, 2024
05f66f2
Commenting out claude for now
calebcapoccia Oct 25, 2024
bcd9e40
Commenting out claude for now, take 2
calebcapoccia Oct 25, 2024
811fa55
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
1f7212e
Fixed issues w/ file path
calebcapoccia Oct 25, 2024
a27a3e9
Fixing
calebcapoccia Oct 25, 2024
4dc2a23
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
13c1866
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
935ee4f
Changing prompt
calebcapoccia Oct 25, 2024
02f2328
Commenting out for now
calebcapoccia Oct 25, 2024
2095ebf
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
d5cb9e2
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
cc21efb
Changing yaml
calebcapoccia Oct 25, 2024
673f3e6
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
4a04629
Overwrite old api.py
calebcapoccia Oct 25, 2024
11b2996
Cleaning things up
calebcapoccia Oct 25, 2024
dcc2e38
Update update_sdk.yaml
calebcapoccia Oct 25, 2024
70e668e
Commenting out diff file
calebcapoccia Oct 25, 2024
73855bd
Keep comments
calebcapoccia Oct 25, 2024
09ff777
Auto-generate SDK and update api.py
calebcapoccia Oct 25, 2024
a103837
Merge pull request #5 from calebcapoccia/auto-generate-sdk
calebcapoccia Oct 25, 2024
70f3587
Adding Claude
calebcapoccia Oct 25, 2024
5a2c041
Adding Claude
calebcapoccia Oct 25, 2024
8d40b7e
Create gen_sdk.yml
calebcapoccia Oct 25, 2024
2e7d72a
Update requirements
calebcapoccia Oct 25, 2024
1968266
using old sdk
calebcapoccia Oct 25, 2024
0400a2c
Trying to fix claude for python
calebcapoccia Oct 25, 2024
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
42 changes: 42 additions & 0 deletions .github/workflows/gen_sdk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Autogenerate SDK (Claude) and Create PR

on:
push:
branches:
- main
paths:
- exa-py/old_openapi.yaml
- exa-py/new_openapi.yaml
workflow_dispatch:

jobs:
generate-sdk:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11' # Use the Python version your project requires

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Run autogen_sdk to update api.py
run: |
python autogen_sdk_claude.py
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}

- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
commit-message: 'Auto-generate SDK (Claude) and update api.py'
title: 'Auto-generate SDK and update api.py'
body: 'This PR contains the updated `api.py` file based on changes in `old_openapi.yaml` and `new_openapi.yaml`.'
branch: 'auto-generate-sdk'
labels: 'autogen, sdk'
43 changes: 43 additions & 0 deletions .github/workflows/update_sdk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Autogenerate SDK and Create PR

on:
push:
branches:
- main
paths:
- exa-py/old_openapi.yaml
- exa-py/new_openapi.yaml
workflow_dispatch:

jobs:
generate-sdk:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11' # Use the Python version your project requires

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Run autogen_sdk to update api.py
run: |
python autogen_sdk.py
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
commit-message: 'Auto-generate SDK and update api.py'
title: 'Auto-generate SDK and update api.py'
body: 'This PR contains the updated `api.py` file based on changes in `old_openapi.yaml` and `new_openapi.yaml`.'
branch: 'auto-generate-sdk'
labels: 'autogen, sdk'

73 changes: 73 additions & 0 deletions autogen_sdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from openai import OpenAI
# import anthropic
import difflib
from dotenv import load_dotenv
import os
import re

load_dotenv()

# Connect to OpenAI
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# claude = anthropic.Client(api_key=os.getenv("ANTHROPIC_API_KEY"))

def read_file(file_path):
with open(file_path, 'r') as file:
return file.read()

# Retrieve old and new openapi.yaml files to use as context - TODO LATER
def retrieve_openapi_files():
old_spec = read_file("old_openapi.yaml")
new_spec = read_file("new_openapi.yaml")
return old_spec, new_spec

def extract_new_sdk(response_text):
pattern = r"\$\$\$(.*?)\$\$\$"
match = re.search(pattern, response_text, re.DOTALL) # re.DOTALL allows matching across multiple lines
if match:
return match.group(1).strip() # Return the content between the $$$ markers
return None

def save_to_file(sdk_content, file_name):
"""
Saves the generated SDK content to a specified file
"""
with open(file_name, 'w') as file:
file.write(sdk_content)

# Use old and new yaml files with current SDK to generate new SDK from new yaml file using 4o
def generate_new_sdk(old_spec, new_spec):

old_sdk = read_file("exa_py/api.py")
prompt = f"The old spec is: {old_spec} \n\n The new spec is: {new_spec} \n\n The old sdk is: {old_sdk} \n\n Please generate a new sdk following the format of the old sdk but using the changes from the new spec. The new SDK MUST follow the same format as the old sdk. In your response, wrap the new sdk in $$$. The new SDK MUST start and end with $$$. Do NOT use backticks in your response. Do not include python ~~~ in your response or wrap the file in anything else inside $$$. Make sure to retain a similar level of comments, updating them to reflect the new changes in the SDK. Do not completely remove comments."

# Make the call to the model
response = client.chat.completions.create(
model="gpt-4o",
messages = [
{"role": "user", "content": prompt}
],
temperature=0.7
)

# Extract the response text
generated_text = response.choices[0].message.content

# save_to_file(generated_text, file_name="full_response.txt")

# Extract the new SDK from the response
new_sdk = extract_new_sdk(generated_text)

save_to_file(new_sdk, file_name="exa_py/api.py")

# Explore the difference
# diff = difflib.unified_diff(old_sdk.splitlines(), new_sdk.splitlines(), fromfile="old_sdk.txt", tofile="new_sdk.txt")
# with open("diffs.txt", 'w') as diff_file:
# diff_file.write("\n".join(diff))


def main():
old_spec, new_spec = retrieve_openapi_files()
generate_new_sdk(old_spec, new_spec)

main()
73 changes: 73 additions & 0 deletions autogen_sdk_claude.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import anthropic
import difflib
from dotenv import load_dotenv
import os
import re

load_dotenv()

# Connect to Claude
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

def read_file(file_path):
with open(file_path, 'r') as file:
return file.read()

# Retrieve old and new openapi.yaml files to use as context
def retrieve_openapi_files():
old_spec = read_file("old_openapi.yaml")
new_spec = read_file("new_openapi.yaml")
return old_spec, new_spec

def extract_new_sdk(response_text):
pattern = r"\$\$\$(.*?)\$\$\$"
match = re.search(pattern, response_text, re.DOTALL) # re.DOTALL allows matching across multiple lines
if match:
return match.group(1).strip() # Return the content between the $$$ markers
return None

def save_to_file(sdk_content, file_name):
"""
Saves the generated SDK content to a specified file
"""
with open(file_name, 'w') as file:
file.write(sdk_content)

# Use old and new yaml files with current SDK to generate new SDK from new yaml file using Claude
def generate_new_sdk(old_spec, new_spec):

old_sdk = read_file("exa_py/api.py")
prompt = f"You are going to develop an SDK for an OpenAPI specification ussing the old spec, new spec, and old sdk. The old spec is: {old_spec} \n\n The new spec is: {new_spec} \n\n The old sdk is: {old_sdk} \n\n Please generate a new sdk following the format of the old sdk but using the changes from the new spec. Use the old SDK as reference, but feel free to change the SDK to be more efficient. In your response, wrap the new sdk in $$$. The new SDK MUST start and end with $$$. Do NOT use backticks in your response. Do not wrap the file in anything else inside $$$. Make sure to retain a similar level of comments, updating them to reflect the new changes in the SDK. Do not completely remove comments. You must keep the old classes and methods. Do not just tell me what would be kept."

# Make the call to the model
message = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=8192,
messages=[
{"role": "user", "content": prompt}
],
temperature=0.7
)

# Extract the response text
generated_text = message.content[0].text

# save_to_file(generated_text, file_name="full_response.txt")

# Extract the new SDK from the response
new_sdk = extract_new_sdk(generated_text)

save_to_file(new_sdk, file_name="exa_py/api.py")


# Explore the difference
# diff = difflib.unified_diff(old_sdk.splitlines(), new_sdk.splitlines(), fromfile="old_sdk.txt", tofile="new_sdk.txt")
# with open("diffs.txt", 'w') as diff_file:
# diff_file.write("\n".join(diff))


def main():
old_spec, new_spec = retrieve_openapi_files()
generate_new_sdk(old_spec, new_spec)

main()
2 changes: 1 addition & 1 deletion exa_py/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,4 +1261,4 @@ def _create_with_tool(
exa_completion = ExaOpenAICompletion.from_completion(
completion=completion, exa_result=exa_result
)
return exa_completion
return exa_completion
Loading