Skip to content

Commit 1bfbbaf

Browse files
committed
first commit
0 parents  commit 1bfbbaf

7 files changed

Lines changed: 142 additions & 0 deletions

File tree

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# `parse_context`
2+
3+
This program uses the OpenAI API, powered by GPT-3, to power the logic of a context-based
4+
active-listening voice assistant. The context of this software is:
5+
6+
- Humans have a conversation, which is recorded and transcribed in real time.
7+
- (**THIS PROGRAM**) The conversation is sent to the OpenAI API along with a few-shot list of examples. The API returns a
8+
detected context, suggestions for what outputs would be useful to the humans, and a list of data sources
9+
for achieving those outputs.
10+
- The voice assistant presents the data to the user to augment the conversation.
11+
12+
## Requirements
13+
14+
- Python 3
15+
- OpenAI Python Client
16+
17+
### Installation
18+
19+
Install Python. Install Pip for Python3. Then run:
20+
21+
```bash
22+
pip3 install -r ./requirements.txt
23+
```
24+
25+
To update the requirements file after changing, you can use:
26+
27+
```bash
28+
pip3 freeze > ./requirements.txt
29+
```
30+
31+
Or
32+
33+
```bash
34+
pip3 freeze | grep openai > ./requirements.txt
35+
```
36+
37+
## Usage
38+
39+
### Interactive Mode
40+
41+
To use interactive mode, make sure the `INTERACTIVE` flag at the top of `parse_context.py` is set to `True`.
42+
43+
Then, run `python3 parse_context.py`. You can enter the conversation on the command line
44+
in the following format, using `/end` to stop.
45+
46+
```
47+
A: Hey, what tech stack do you think we should use for the new app?
48+
B: What app?
49+
A: The dating app we are developing. It needs to be cross-platform mobile.
50+
B: Oh, okay. Um... I don't really know any good frameworks
51+
/end
52+
```
53+
54+
The result will be printed to `STDOUT` (default is the terminal window).
55+
56+
### Non-interactive mode
57+
58+
To use non-interactive mode, make sure the `INTERACTIVE` flag at the top of `parse_context.py` is set to `False`.
59+
60+
Then, run the program while passing your conversation to standard input (`STDIN`). For example, if the above
61+
conversation were placed in a file located at `./conversations/tech_stack.txt`, we could run:
62+
63+
```bash
64+
cat ./conversations/tech_stack.txt | python3 parse_context.py
65+
```
66+
67+
And the result will be printed to `STDOUT`.

conversations/actor.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A: I watched Terminator last night.
2+
B: Oh, cool! I love that movie, it's a classic.
3+
A: Arnold Schwarzenegger was so good.
4+
B: Yeah, I agree.

conversations/cooking.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A: Hey, what do you think we should cook for dinner tonight?
2+
B: Dunno. Let's check the fridge.
3+
A: Okay. Looks like we've got chicken, beef, lettuce, rice, mustard, mayo, taco shells, and corn.
4+
B: How about tacos?

conversations/dinner.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A: Hey Mike, let's get dinner tonight.
2+
B: Okay, sure! What restaurants do you know in the area?

conversations/tech_stack.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A: Hey, what tech stack do you think we should use for the new app?
2+
B: What app?
3+
A: The dating app we are developing. It needs to be cross-platform mobile.
4+
B: Oh, okay. Um... I don't really know any good frameworks

parse_context.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Author: Radu Vasilescu
3+
Date: 2020-11-10
4+
"""
5+
6+
import os
7+
import sys
8+
import openai
9+
10+
"""
11+
Determines the input method for the script.
12+
13+
A value of True reads the program's input from the command line interactively.
14+
A value of False reads the program's input from the process' STDIN. An example of
15+
non-interactive usage is:
16+
17+
cat ./conversations/dinner.txt | python3 parse_content.py
18+
"""
19+
INTERACTIVE : bool = False
20+
21+
openai.api_key : str = os.environ["OPENAI_API_KEY"]
22+
23+
# Set up 4-shot example prompts:
24+
examples : str = "The following is a list of conversations between humans. A voice-activated AI suggests information that might be relevant in the context of the conversation.\n\nA: I'm going to leave for work soon.\nB: Okay, I'm leaving too.\nA: Let me get dressed first.\nContext: Two people are about to leave for work.\nUseful Output: Current weather, current traffic, estimated commute time, reminders\nRelevant Data Sources: Query Google Weather API to get current weather, query Google Maps API to get traffic and commute time, query Reminders app to get reminders.\n\nA: Have you seen that movie, Avengers Endgame?\nB: Oh, yeah! I saw it when it came out. I loved Iron Man in that series!\nA: Who played him again? \nB: I don't remember.\nContext: Two people are wondering who played Iron Man in the film Avengers Endgame\nUseful Output: Tony Stark\nRelevant Data Sources: Query Google Search for \"Who plays Iron Man in Avengers Endgame?\"\n\nA: So, when do you guys want to meet up later?\nB: I don't know, I have a pretty busy evening\nC: I'm free after 5:00pm\nA: Let me look at my schedule.\nContext: A group of people is trying to schedule an event this evening.\nUseful Output: Today's remaining calendar events.\nRelevant Data Sources: Query Google Calendar API for events after 5pm today.\n\nA: I have to pick my daughter up from school today.\nB: Okay, what time is that going to be?\nContext: Two people are talking about a plan to pick someone up from school\nUseful Output: Reminder to pick up the child, current traffic conditions to the school, estimated time to the school\nRelevant Data Sources: Add a reminder to the Reminders app, query Google Maps API for current traffic and school location, query Google Maps API for estimated travel time\n\n"
25+
26+
start_sequence : str = "Context:"
27+
28+
conversation : str = ""
29+
if INTERACTIVE:
30+
# Read the conversation from the console
31+
print('Enter conversation. Use /end to signal the end.\n')
32+
33+
while (line := input()) != '/end':
34+
conversation += line + "\n"
35+
else:
36+
# Read the conversation from standard input
37+
for line in sys.stdin:
38+
if line == "\n" or line == "/end":
39+
print("Do not include neither newlines nor /end in the input (INTERACTIVE = %s)", str(INTERACTIVE))
40+
exit(1)
41+
conversation += line
42+
43+
print('Generating completion...')
44+
45+
response = openai.Completion.create(
46+
engine = "davinci",
47+
prompt = examples + conversation + start_sequence,
48+
temperature = 0.7,
49+
max_tokens = 64,
50+
top_p = 1,
51+
stop = ["\n\n"]
52+
)
53+
54+
response_text : str = response["choices"][0]["text"]
55+
response_split = response_text.split("\n")
56+
gen_context, gen_output, gen_sources = response_split[0].strip(), response_split[1].strip(), response_split[2].strip()
57+
58+
print('Context: ' + gen_context)
59+
print(gen_output)
60+
print(gen_sources)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
openai==0.2.6

0 commit comments

Comments
 (0)