Create GPT Class for Text Generation
- Dominant language
- Python
- Stars
- 2
- Forks
- 0
- Avg merge
- 1m
- Merged PRs (30d)
- 1
Description
Here is starter code:
import openai
class GPTTextCompletion:
def __init__(self, api_key, model="text-davinci-003", max_tokens=100, temperature=0.7):
"""
Initialize the GPTTextCompletion class with API key, model, and hyperparameters.
:param api_key: OpenAI API key for authentication.
:param model: Model to use for text generation.
:param max_tokens: Maximum number of tokens to generate.
:param temperature: Sampling temperature to use.
"""
self.api_key = api_key
openai.api_key = self.api_key
self.model = model
self.max_tokens = max_tokens
self.temperature = temperature
self.prompt = ""
self.last_completion = None
def generate_text(self, prompt=None, n=1):
"""
Generate text completion based on the given prompt.
:param prompt: Text prompt for the model (if None, use the initialized prompt).
:param n: Number of completions to generate.
:return: The best generated text completion.
"""
if prompt is None:
prompt = self.prompt
try:
response = openai.Completion.create(
model=self.model,
prompt=prompt,
max_tokens=self.max_tokens,
temperature=self.temperature,
n=n
)
completions = [choice.text.strip() for choice in response.choices]
self.last_completion = max(completions, key=len) # Assuming the best completion is the longest one
return self.last_completion
except Exception as e:
print(f"Error generating text: {e}")
return None
def get_last_completion(self):
"""
Get the last generated text completion.
:return: Last generated text completion.
"""
return self.last_completion
def engineer_prompt(self, context, task, details=None, format_specification=None):
"""
Engineer a prompt based on provided context, task, and optional details and format specification.
:param context: Background information or context for the prompt.
:param task: The main task or question for the prompt.
:param details: Additional details or requirements for the prompt.
:param format_specification: Format specifications for the output.
"""
self.prompt = f"Context: {context}\n"
self.prompt += f"Task: {task}\n"
if details:
self.prompt += f"Details: {details}\n"
if format_specification:
self.prompt += f"Format: {format_specification}\n"
self.prompt += "Response: "
# Usage example
if __name__ == "__main__":
api_key = "YOUR_OPENAI_API_KEY"
# Initialize the class with default parameters
gpt = GPTTextCompletion(
api_key=api_key,
model="text-davinci-003",
max_tokens=150,
temperature=0.7
)
# Use the prompt engineering method to create a prompt and set it as the class attribute
context = "You are an expert in machine learning."
task = "Explain the concept of overfitting."
details = "Include examples and methods to prevent it."
format_specification = "Provide the response in a structured format with bullet points."
gpt.engineer_prompt(context, task, details, format_specification)
# Generate text using the engineered prompt
best_completion = gpt.generate_text(n=3)
# Print the best completion
print("Best Completion:\n", best_completion)
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.