rashid01 commited on
Commit
0417a77
·
verified ·
1 Parent(s): ae527e2

Create q2.py

Browse files
Files changed (1) hide show
  1. q2.py +48 -0
q2.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+
3
+ def generate_text_completion(api_key, prompt, model="text-davinci-003", max_tokens=100, temperature=0.5):
4
+ """
5
+ Generate a text completion using LangChain (GPT-3 model).
6
+
7
+ Parameters:
8
+ - api_key (str): Your OpenAI API key.
9
+ - prompt (str): The prompt text to generate completion from.
10
+ - model (str): The GPT-3 model to use. Defaults to 'text-davinci-003'.
11
+ - max_tokens (int): Maximum number of tokens (words) in the generated text. Defaults to 100.
12
+ - temperature (float): Controls randomness in text generation. Higher values make the output more diverse. Defaults to 0.5.
13
+
14
+ Returns:
15
+ - generated_text (str): The generated text completion.
16
+ """
17
+ # Set the OpenAI API key
18
+ openai.api_key = api_key
19
+
20
+ # Generate text completion using LangChain
21
+ response = openai.Completion.create(
22
+ engine=model,
23
+ prompt=prompt,
24
+ max_tokens=max_tokens,
25
+ temperature=temperature
26
+ )
27
+
28
+ # Extract and return the generated text
29
+ generated_text = response['choices'][0]['text'].strip()
30
+ return generated_text
31
+
32
+ # Example usage:
33
+ def main():
34
+ # Replace 'your_openai_api_key_here' with your actual API key
35
+ api_key = 'your_openai_api_key_here'
36
+
37
+ # Example prompt
38
+ prompt = "Once upon a time in a galaxy far, far away"
39
+
40
+ # Generate text completion
41
+ generated_text = generate_text_completion(api_key, prompt)
42
+
43
+ # Print generated text
44
+ print("Generated Text Completion:")
45
+ print(generated_text)
46
+
47
+ if __name__ == "__main__":
48
+ main()