Skip to content

Commit 21abbae

Browse files
authored
PG-541: Adding /detokenize function and test along with audio diarization and timestamps (#42)
* detokenize func and tests and audio updated * fixing type error * fixing audio type error * Update audio.py * updating examples and docs
1 parent 9b60ca5 commit 21abbae

34 files changed

+1151
-251
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ celerybeat-schedule
8484

8585
# Environments
8686
.env
87+
.envrc
8788
.venv
8889
env/
8990
venv/

docs/source/quick_start.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ To use this library, you must have an api key. You can set it two ways: as an en
1616
from predictionguard import PredictionGuard
1717
1818
19-
# You can set you Prediction Guard API Key as an env variable,
20-
# or when creating the client object
21-
os.environ["PREDICTIONGUARD_API_KEY"]
19+
# Set your Prediction Guard token and url as an environmental variable.
20+
os.environ["PREDICTIONGUARD_API_KEY"] = "<api key>"
21+
os.environ["PREDICTIONGUARD_URL"] = "<url>"
2222
23-
client = PredictionGuard(
24-
api_key="<your Prediction Guard API Key>"
25-
)
23+
# Or set your Prediction Guard token and url when initializing the PredictionGuard class.
24+
client = PredictionGuard(
25+
api_key=<api_key>,
26+
url=<url>
27+
)
2628
2729
messages = [
2830
{
@@ -36,9 +38,8 @@ To use this library, you must have an api key. You can set it two ways: as an en
3638
]
3739
3840
result = client.chat.completions.create(
39-
model="Hermes-2-Pro-Llama-3-8B",
40-
messages=messages,
41-
max_tokens=100
41+
model="Hermes-3-Llama-3.1-8B",
42+
messages=messages
4243
)
4344
4445
print(json.dumps(

docs/source/requirements.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Requirements
22
=================
33

4-
To access the API, contact us `here <https://mailchi.mp/predictionguard/getting-started>`_ to get an enterprise access token. You will need this access token to continue.
4+
To access the API, you will need an API Key. Contact us `here <https://predictionguard.com/get-started>`_ to get started.

examples/audio.ipynb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {},
5+
"cell_type": "markdown",
6+
"source": "## Transcribing Audio with Prediction Guard",
7+
"id": "53b2be3dbc44dbf2"
8+
},
9+
{
10+
"metadata": {},
11+
"cell_type": "markdown",
12+
"source": "### Setup",
13+
"id": "ea9357a03d7869da"
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "initial_id",
19+
"metadata": {
20+
"collapsed": true
21+
},
22+
"outputs": [],
23+
"source": [
24+
"# Import necessary packages\n",
25+
"import os\n",
26+
"import json\n",
27+
"\n",
28+
"from predictionguard import PredictionGuard\n",
29+
"\n",
30+
"\n",
31+
"# Set your Prediction Guard token and url as an environmental variable.\n",
32+
"os.environ[\"PREDICTIONGUARD_API_KEY\"] = \"<api key>\"\n",
33+
"os.environ[\"PREDICTIONGUARD_URL\"] = \"<url>\"\n",
34+
"\n",
35+
"# Or set your Prediction Guard token and url when initializing the PredictionGuard class.\n",
36+
"client = PredictionGuard(\n",
37+
" api_key=\"<api_key>\",\n",
38+
" url=\"<url>\"\n",
39+
")"
40+
]
41+
},
42+
{
43+
"metadata": {},
44+
"cell_type": "markdown",
45+
"source": "### Transcribe Audio",
46+
"id": "65ffcefb7e8c4f73"
47+
},
48+
{
49+
"metadata": {},
50+
"cell_type": "code",
51+
"outputs": [],
52+
"execution_count": null,
53+
"source": [
54+
"response = client.audio.transcriptions.create(\n",
55+
" model=\"base\",\n",
56+
" file=\"sample_audio.wav\"\n",
57+
")\n",
58+
"\n",
59+
"print(json.dumps(\n",
60+
" response,\n",
61+
" sort_keys=True,\n",
62+
" indent=4,\n",
63+
" separators=(\",\", \": \")\n",
64+
"))"
65+
],
66+
"id": "1b6769e1b2e6bd6b"
67+
}
68+
],
69+
"metadata": {
70+
"kernelspec": {
71+
"display_name": "Python 3",
72+
"language": "python",
73+
"name": "python3"
74+
},
75+
"language_info": {
76+
"codemirror_mode": {
77+
"name": "ipython",
78+
"version": 2
79+
},
80+
"file_extension": ".py",
81+
"mimetype": "text/x-python",
82+
"name": "python",
83+
"nbconvert_exporter": "python",
84+
"pygments_lexer": "ipython2",
85+
"version": "2.7.6"
86+
}
87+
},
88+
"nbformat": 4,
89+
"nbformat_minor": 5
90+
}

examples/chat.ipynb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@
2727
"from predictionguard import PredictionGuard\n",
2828
"\n",
2929
"\n",
30-
"# Set PG API Key\n",
30+
"# Set your Prediction Guard token and url as an environmental variable.\n",
3131
"os.environ[\"PREDICTIONGUARD_API_KEY\"] = \"<api key>\"\n",
32+
"os.environ[\"PREDICTIONGUARD_URL\"] = \"<url>\"\n",
3233
"\n",
33-
"# Initialize PG client\n",
34-
"client = PredictionGuard()"
34+
"# Or set your Prediction Guard token and url when initializing the PredictionGuard class.\n",
35+
"client = PredictionGuard(\n",
36+
" api_key=\"<api_key>\",\n",
37+
" url=\"<url>\"\n",
38+
")"
3539
]
3640
},
3741
{
@@ -59,9 +63,9 @@
5963
"]\n",
6064
"\n",
6165
"chat_response = client.chat.completions.create(\n",
62-
" model=\"Hermes-2-Pro-Mistral-7B\",\n",
66+
" model=\"Hermes-3-Llama-3.1-8B\",\n",
6367
" messages=messages,\n",
64-
" max_tokens=500,\n",
68+
" max_completion_tokens=500,\n",
6569
" temperature=1.0,\n",
6670
" top_p=1.0,\n",
6771
" top_k=50\n",
@@ -95,14 +99,14 @@
9599
" },\n",
96100
" {\n",
97101
" \"role\": \"user\",\n",
98-
" \"content\": \"Write me a childrens story about an elf warrior.\"\n",
102+
" \"content\": \"Write me a children's story about an elf warrior.\"\n",
99103
" }\n",
100104
"]\n",
101105
"\n",
102106
"for res in client.chat.completions.create(\n",
103-
" model=\"Hermes-2-Pro-Mistral-7B\",\n",
107+
" model=\"Hermes-3-Llama-3.1-8B\",\n",
104108
" messages=messages,\n",
105-
" max_tokens=100,\n",
109+
" max_completion_tokens=100,\n",
106110
" stream=True\n",
107111
"):\n",
108112
" # Use 'end' parameter in print function to avoid new lines.\n",
@@ -143,7 +147,7 @@
143147
"]\n",
144148
"\n",
145149
"vision_response = client.chat.completions.create(\n",
146-
" model=\"llava-1.5-7b-hf\",\n",
150+
" model=\"Qwen2.5-VL-7B-Instruct\",\n",
147151
" messages=messages\n",
148152
")\n",
149153
"\n",

examples/completions.ipynb

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
{
1111
"cell_type": "markdown",
1212
"metadata": {},
13-
"source": [
14-
"### Setup"
15-
]
13+
"source": "### Setup"
1614
},
1715
{
1816
"cell_type": "code",
19-
"execution_count": null,
2017
"metadata": {},
21-
"outputs": [],
2218
"source": [
2319
"# Import necessary packages\n",
2420
"import os\n",
@@ -27,12 +23,19 @@
2723
"from predictionguard import PredictionGuard\n",
2824
"\n",
2925
"\n",
30-
"# Set PG API Key\n",
26+
"\n",
27+
"# Set your Prediction Guard token and url as an environmental variable.\n",
3128
"os.environ[\"PREDICTIONGUARD_API_KEY\"] = \"<api key>\"\n",
29+
"os.environ[\"PREDICTIONGUARD_URL\"] = \"<url>\"\n",
3230
"\n",
33-
"# Initialize PG client\n",
34-
"client = PredictionGuard()"
35-
]
31+
"# Or set your Prediction Guard token and url when initializing the PredictionGuard class.\n",
32+
"client = PredictionGuard(\n",
33+
" api_key=\"Bg-98uZ5mJPEwFQJE8UN9MuG6KG2SK9gJILyw3nYPFA\",\n",
34+
" url=\"<url>\"\n",
35+
")"
36+
],
37+
"outputs": [],
38+
"execution_count": null
3639
},
3740
{
3841
"cell_type": "markdown",
@@ -43,14 +46,12 @@
4346
},
4447
{
4548
"cell_type": "code",
46-
"execution_count": null,
4749
"metadata": {},
48-
"outputs": [],
4950
"source": [
5051
"response = client.completions.create(\n",
51-
" model=\"Hermes-2-Pro-Mistral-7B\",\n",
52-
" messages=\"Tell me a joke.\",\n",
53-
" max_tokens=500\n",
52+
" model=\"Hermes-3-Llama-3.1-8B\",\n",
53+
" prompt=\"Tell me a joke.\",\n",
54+
" max_tokens=100\n",
5455
")\n",
5556
"\n",
5657
"print(json.dumps(\n",
@@ -59,7 +60,30 @@
5960
" indent=4,\n",
6061
" separators=(',', ': ')\n",
6162
"))"
62-
]
63+
],
64+
"outputs": [],
65+
"execution_count": null
66+
},
67+
{
68+
"metadata": {},
69+
"cell_type": "markdown",
70+
"source": "### Streaming Completions"
71+
},
72+
{
73+
"metadata": {},
74+
"cell_type": "code",
75+
"source": [
76+
"for res in client.completions.create(\n",
77+
" model=\"Hermes-3-Llama-3.1-8B\",\n",
78+
" prompt=\"Tell me a joke.\",\n",
79+
" max_tokens=100,\n",
80+
" stream=True\n",
81+
"):\n",
82+
" # Use 'end' parameter in print function to avoid new lines.\n",
83+
" print(res[\"data\"][\"choices\"][0][\"text\"], end=\"\")"
84+
],
85+
"outputs": [],
86+
"execution_count": null
6387
},
6488
{
6589
"cell_type": "markdown",
@@ -70,14 +94,14 @@
7094
},
7195
{
7296
"cell_type": "code",
73-
"execution_count": null,
7497
"metadata": {},
75-
"outputs": [],
7698
"source": [
7799
"model_list = client.completions.list_models()\n",
78100
"\n",
79101
"print(model_list)"
80-
]
102+
],
103+
"outputs": [],
104+
"execution_count": null
81105
}
82106
],
83107
"metadata": {

0 commit comments

Comments
 (0)