-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatbot.py
More file actions
84 lines (68 loc) · 2.34 KB
/
Chatbot.py
File metadata and controls
84 lines (68 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import google.generativeai as genai
import google.api_core.exceptions
genai.configure(api_key="AIzaSyCr2RdUvkM0FDn8-Am-rwVJXYygfty5OUo")
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-pro",
generation_config=generation_config,
# safety_settings = Adjust safety settingsw
# See https://ai.google.dev/gemini-api/docs/safety-settings
system_instruction="You are an AI powered chatbot to provide mentalhealth and emotional support to students",
)
history = []
while True:
chat_session = model.start_chat(history = history)
u = input("User: ")
try:
model_response = chat_session.send_message(u)
response = model_response.text
except google.api_core.exceptions.InternalServerError as e:
print("Try again")
continue
print("\nChatbot: ", response)
if u in ["quit", "exit", "bye"]:
break
history.append({
"role": "user",
"parts": [u,],
})
history.append({
"role": "model",
"parts": [response,],
})
# from flask import Flask, request, jsonify
# import google.generativeai as genai
# import google.api_core.exceptions
# app = Flask(__name__)
# genai.configure(api_key="AIzaSyCr2RdUvkM0FDn8-Am-rwVJXYygfty5OUo")
# generation_config = {
# "temperature": 1,
# "top_p": 0.95,
# "top_k": 64,
# "max_output_tokens": 8192,
# "response_mime_type": "text/plain",
# }
# model = genai.GenerativeModel(
# model_name="gemini-1.5-pro",
# generation_config=generation_config,
# system_instruction="You are an AI powered chatbot to provide mental health and emotional support to students",
# )
# @app.route('/chat', methods=['POST'])
# def chat():
# user_input = request.json.get('prompt')
# history = [] # You may want to manage history here
# try:
# chat_session = model.start_chat(history=history)
# model_response = chat_session.send_message(user_input)
# response = model_response.text
# return jsonify({"response": response})
# except google.api_core.exceptions.InternalServerError:
# return jsonify({"error": "Internal Server Error"}), 500
# if __name__ == '__main__':
# app.run(debug=True)