Skip to content

Commit 5a5062b

Browse files
authored
Merge pull request #42 from abhiram304/feature/addopenai
Feature/addopenai
2 parents 959d866 + eb2f696 commit 5a5062b

File tree

7 files changed

+207
-72
lines changed

7 files changed

+207
-72
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ EMAIL_HOST_USER = '<smtp host user>'
4141
EMAIL_HOST_PASSWORD = '<smtp host pass>'
4242
DEFAULT_FROM_EMAIL = '<from email address>'
4343
```
44+
## Getting an OpenAI API Key
45+
46+
1. **Sign Up for an OpenAI Account:**
47+
- Go to [OpenAI’s website](https://www.openai.com/) and sign up for an account if you don't have one.
48+
49+
2. **Navigate to API Keys:**
50+
- After logging in, go to the API section of your account by visiting [OpenAI API Keys](https://platform.openai.com/account/api-keys).
51+
52+
3. **Create a New API Key:**
53+
- Click on the "Create new key" button. A new API key will be generated for you.
54+
55+
4. **Copy Your API Key:**
56+
- Copy the API key to your clipboard. You will need this key to access OpenAI services.
57+
58+
5. **Set Up Your Environment:**
59+
- Add the API key to your environment variables. You can do this by setting an environment variable in your terminal or adding it to a `.env` file or add in settings.py . For example, you can set it in your `.env` file like this:
60+
```
61+
OPENAI_API_KEY=your_api_key_here
62+
```
63+
4464
<h2> Configuring OAuth login </h2>
4565
<details>
4666
<summary>Obtaining OAuth Client ID for Google</summary>

pollme/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,5 @@
152152
EMAIL_HOST_USER = 'TBD'
153153
EMAIL_HOST_PASSWORD = 'TBD'
154154
DEFAULT_FROM_EMAIL = 'TBD' # The email address you want to send from
155+
156+
OPENAI_API_KEY = 'your_open_ai_key'

polls/templates/polls/chat.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
5+
<div class="container">
6+
<div class="row">
7+
<div class="col-md-8 offset-sm-2">
8+
<h1 class="text-center mb-5">Ask Open AI</h1>
9+
<div class="table-responsive">
10+
<form id="chatForm" class="form-group">
11+
<label for="userInput">Your Question:</label>
12+
<textarea id="userInput" class="form-control mb-3" rows="4" placeholder="What poll i can create on health care topic ?"></textarea>
13+
<button type="button" id="sendButton" class="btn btn-primary" onclick="sendMessage()">Send</button>
14+
</form>
15+
<label id="responseLabel" class="mt-3" style="white-space:pre-line"></label>
16+
</div>
17+
</div>
18+
</div>
19+
</div>
20+
21+
<script>
22+
function sendMessage() {
23+
const text = document.getElementById('userInput').value;
24+
25+
// Disable submit button
26+
document.getElementById('sendButton').disabled = true;
27+
28+
fetch('/polls/chat/', {
29+
method: 'POST',
30+
headers: {'Content-Type': 'application/json'},
31+
body: JSON.stringify({message: text})
32+
})
33+
.then(response => response.json())
34+
.then(data => {
35+
// Clear input value
36+
document.getElementById('userInput').value = '';
37+
38+
document.getElementById('responseLabel').textContent = data.reply;
39+
}).finally(() => {
40+
// Enable submit button
41+
document.getElementById('sendButton').disabled = false;
42+
})
43+
}
44+
</script>
45+
46+
{% endblock content %}

polls/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
path('<int:poll_id>/', views.poll_detail, name='detail'),
1818
path('<int:poll_id>/vote/', views.poll_vote, name='vote'),
1919
path('dashboard/', views.dashboard, name='dashboard'),
20+
path('chat/', views.chat_view, name='chat_view'),
21+
path('chat_page/', views.chat_page, name='chat_page'),
2022
]

0 commit comments

Comments
 (0)