Last active
August 29, 2023 21:55
-
-
Save ddrscott/08fb103fc3e85bcb53fb7882837066f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Requirements: | |
pip install click langchain openai | |
""" | |
import sys | |
import click | |
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler | |
class CustomHandler(StreamingStdOutCallbackHandler): | |
def on_llm_start(self, serialized, prompts, **_) -> None: | |
pass | |
def on_llm_new_token(self, token: str, **_) -> None: | |
click.echo(token, nl=False) | |
def on_llm_end(self, response, **kwargs) -> None: | |
click.echo('\n') | |
from langchain.chat_models import ChatOpenAI | |
def auto_lint(data, model): | |
llm=ChatOpenAI( | |
client=None, | |
model=model, | |
temperature=0.1, | |
verbose=True, | |
callbacks=[CustomHandler()], | |
streaming=True, | |
) | |
llm.predict(f"""You are an expert Python developer. | |
Please make the following updates to the attached code: | |
- add useful Google style docstrings to functions. | |
- fix spelling mistakes. | |
- strip whitespace. | |
```python | |
{data} | |
``` | |
Updated Python code:""") | |
@click.command() | |
@click.option('--model', '-m', default='gpt-3.5-turbo-16k-0613') | |
@click.argument('src', | |
type=click.File('r'), | |
default=sys.stdin) | |
def my_command(model, src): | |
data = None | |
with src: | |
data = src.read() | |
auto_lint(data, model) | |
if __name__ == '__main__': | |
my_command() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Use:
Example output when used on itself: