Last active
May 13, 2024 19:31
-
-
Save afiodorov/bb547f1c750eaa717e3997fd7a6e0115 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
#!/usr/bin/env python3 | |
import os | |
import openai | |
import argparse | |
import sys | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
def get_completion(instruction): | |
return openai.Completion.create( | |
model="text-davinci-003", | |
prompt=instruction + "\n", | |
temperature=0, | |
max_tokens=1000, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0, | |
)["choices"][0]["text"].strip() | |
def main(): | |
parser = argparse.ArgumentParser(description="Ask GPT-3.") | |
parser.add_argument("prompt", nargs="?", type=str, default=None) | |
parser.add_argument( | |
"-i", | |
"--input", | |
type=argparse.FileType("r"), | |
help="input file", | |
default=sys.stdin, | |
) | |
parser.add_argument( | |
"-o", | |
"--output", | |
type=argparse.FileType("w"), | |
default=sys.stdout, | |
help="output file", | |
) | |
args = parser.parse_args() | |
if args.prompt: | |
print(get_completion(args.prompt), file=args.output) | |
return | |
lines = [] | |
for line in args.input: | |
lines.append(line) | |
input_ = "".join(lines) | |
print(get_completion(input_), file=args.output) | |
if __name__ == "__main__": | |
main() |
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
import argparse | |
import asyncio | |
import sys | |
import openai | |
async def get_completion(instruction: str)->str: | |
client = openai.AsyncClient() | |
response = await client.chat.completions.create( | |
model="gpt-4o", | |
messages=[{"role": "user", "content": instruction}], | |
temperature=0, | |
) | |
return response.choices[0].message.content or "" | |
async def main(): | |
parser = argparse.ArgumentParser(description="Ask GPT-4o.") | |
parser.add_argument("prompt", nargs="?", type=str, default=None) | |
parser.add_argument( | |
"-i", | |
"--input", | |
type=argparse.FileType("r"), | |
help="input file", | |
default=sys.stdin, | |
) | |
parser.add_argument( | |
"-o", | |
"--output", | |
type=argparse.FileType("w"), | |
default=sys.stdout, | |
help="output file", | |
) | |
args = parser.parse_args() | |
if args.prompt: | |
print(await get_completion(args.prompt), file=args.output) | |
return | |
lines = [] | |
for line in args.input: | |
lines.append(line) | |
input_ = "".join(lines) | |
print(await get_completion(input_), file=args.output) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment