openai_api.py 637 B

12345678910111213141516171819
  1. import openai
  2. from transformers import GPT2Tokenizer
  3. openai.api_key = "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"
  4. def get_reply(message):
  5. reply = openai.ChatCompletion.create(model="gpt-4-0314",
  6. messages=[{
  7. "role": "user",
  8. "content": message
  9. }])
  10. return reply["choices"][0]["message"]["content"]
  11. def get_tokens_length(message):
  12. tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
  13. tokens = tokenizer.encode(message)
  14. return len(tokens)