You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You need to create a virtual env and install the packages listed in `requirements.txt`. You can then run Jupyter Notebooks in VS Code.
12
+
13
+
Follow these steps: [How to Work with Python Virtual Environments, Jupyter Notebooks and VS Code](https://python.plainenglish.io/how-to-work-with-python-virtual-environments-jupyter-notebooks-and-vs-code-536fac3d93a1).
14
+
15
+
You need to create a `.env` file with your `OPENAI_API_KEY`.
16
+
17
+
# Usage
18
+
19
+
Open `F1_QA_Assistant.ipynb`.
20
+
21
+
## Features
22
+
23
+
- scraping data from Wikipedia.
24
+
- generating a bunch of embeddings on the last Formula One season.
25
+
- turning the questions from users into embeddings.
26
+
- finding the K nearest neighbors to that embedding.
27
+
- including the matching texts in the prompt to expand GPT-4 knowledge.
28
+
29
+
Based on [Mastering OpenAI Python APIs: Unleash the Power of GPT4](https://www.udemy.com/course/mastering-openai/) by Colt Steele (2023).
tokens_per_message=4# every message follows <|start|>{role/name}\n{content}<|end|>\n
46
+
tokens_per_name=-1# if there's a name, the role is omitted
47
+
elifmodel=="gpt-4-0314":
48
+
tokens_per_message=3
49
+
tokens_per_name=1
50
+
else:
51
+
raiseNotImplementedError(
52
+
f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens."""
53
+
)
54
+
num_tokens=0
55
+
formessageinmessages:
56
+
num_tokens+=tokens_per_message
57
+
forkey, valueinmessage.items():
58
+
num_tokens+=len(encoding.encode(value))
59
+
ifkey=="name":
60
+
num_tokens+=tokens_per_name
61
+
num_tokens+=3# every reply is primed with <|start|>assistant<|message|>
62
+
returnnum_tokens
63
+
64
+
65
+
defmemoize_to_sqlite(filename: str="cache.db"):
66
+
"""
67
+
Memoization decorator that caches the output of a method in a SQLite database.
68
+
The database connection is persisted across calls.
69
+
"""
70
+
db_conn=sqlite3.connect(filename)
71
+
db_conn.execute("CREATE TABLE IF NOT EXISTS cache (hash TEXT PRIMARY KEY, result TEXT)")
0 commit comments