ajaypandey1585 commited on
Commit
7ce578b
1 Parent(s): c877b01

main-non-upgraded.py

Browse files
Files changed (1) hide show
  1. main-non-upgraded.py +68 -0
main-non-upgraded.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # main.py
2
+
3
+ from dotenv import load_dotenv
4
+ import os
5
+ from openai import OpenAI
6
+
7
+ client = OpenAI(api_key='sk-proj-PxCAkOqCTsVhVWTJSMKJT3BlbkFJz0J48QsSGmrt9Qjud2Sl')
8
+ import pprint
9
+ from halo import Halo
10
+
11
+ # Load environment variables from a .env file
12
+ load_dotenv()
13
+
14
+ # Function to generate a response from the model
15
+ def generate_response(messages):
16
+ # Create a loading spinner
17
+ spinner = Halo(text='Loading...', spinner='dots')
18
+ spinner.start()
19
+
20
+ # Load the OpenAI key and model name from environment variables
21
+ model_name = 'gpt-3.5-turbo-0301'
22
+
23
+ # Create a chat completion with the provided messages
24
+ response = client.chat.completions.create(model=model_name,
25
+ messages=messages,
26
+ temperature=0.5,
27
+ max_tokens=250)
28
+
29
+ # Stop the spinner once the response is received
30
+ spinner.stop()
31
+
32
+ # Pretty-print the messages sent to the model
33
+ pp = pprint.PrettyPrinter(indent=4)
34
+ print("Request:")
35
+ pp.pprint(messages)
36
+
37
+ # Print the usage statistics for the completion
38
+ print(f"Completion tokens: {response.usage.completion_tokens}, Prompt tokens: {response.usage.prompt_tokens}, Total tokens: {response.usage.total_tokens}")
39
+
40
+ # Return the message part of the response
41
+ return response.choices[0].message
42
+
43
+ # Main function to run the chatbot
44
+ def main():
45
+ # Initialize the messages with a system message
46
+ messages=[
47
+ {"role": "system", "content": "You are a kind and wise wizard"}
48
+ ]
49
+
50
+ # Continue chatting until the user types "quit"
51
+ while True:
52
+ input_text = input("You: ")
53
+ if input_text.lower() == "quit":
54
+ break
55
+
56
+ # Add the user's message to the messages
57
+ messages.append({"role": "user", "content": input_text})
58
+
59
+ # Get a response from the model and add it to the messages
60
+ response = generate_response(messages)
61
+ messages.append(response)
62
+
63
+ # Print the assistant's response
64
+ print(f"Wizard: {response.content}")
65
+
66
+ # Run the main function when the script is run
67
+ if __name__ == "__main__":
68
+ main()