Skip to content

Instantly share code, notes, and snippets.

@david-strejc
Created April 16, 2023 18:30
Show Gist options
  • Save david-strejc/006dd3887ded389ce4bec2a11b1da28d to your computer and use it in GitHub Desktop.
Save david-strejc/006dd3887ded389ce4bec2a11b1da28d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
import json
# Define your API key here. Replace "your-api-key" with your actual API key.
api_key = "your-api-key"
# Define the ChatGPT API endpoint.
url = "https://api.openai.com/v1/engines/davinci-codex/completions"
# Define the long context as a string.
long_context = """
<very long context goes here>
"""
# Define a function to send a message to the ChatGPT API.
def send_message_to_chatgpt(prompt, context=None):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
data = {
"prompt": f"{context} {prompt}" if context else prompt,
"max_tokens": 150,
"n": 1,
"stop": None,
"temperature": 0.7,
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
result = response.json()
return result["choices"][0]["text"].strip()
else:
print(f"Error {response.status_code}: {response.text}")
return None
# Summarize the long context using ChatGPT.
summarize_prompt = f"Please provide a concise summary of the following text:\n{long_context}"
short_context = send_message_to_chatgpt(summarize_prompt)
# Example usage with the summarized context:
prompt = "What is the main point of the summarized text?"
response_text = send_message_to_chatgpt(prompt, context=short_context)
print(response_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment