Skip to content

Commit d667b6d

Browse files
committed
better examples including python example
1 parent cd61c93 commit d667b6d

File tree

14 files changed

+487
-78
lines changed

14 files changed

+487
-78
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Basic caching proxy for OpenAI API, deployable as a [Cloudflare Worker](https://
55
This can help you reduce your OpenAI costs (and get faster results) by returning cached responses for repeated requests.
66

77
**Client compatibility:**
8+
89
- [openai/openai-node](https://github.com/openai/openai-node): full compatibility, takes just a few lines of config to use
910
- [openai/openai-python](https://github.com/openai/openai-python): partial compatibility, supports caching but no TTL options so you'll need a cache eviction policy
1011

@@ -85,6 +86,8 @@ const configuration = new Configuration({
8586
});
8687
```
8788

88-
### Samples
89+
### Example client usage
90+
91+
See `/examples/` directory for a full example of how to call this proxy with your openai client.
8992

90-
See `/samples/sample-usage.ts` for a full example of how to call this proxy with your openai client.
93+
This includes both Node.js and Python client usage examples.

examples/node-js/.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Create a file called ".env" and add this env var:
2+
OPENAI_API_KEY=...insertme...

examples/node-js/.nvmrc

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

samples/sample-usage.ts renamed to examples/node-js/example.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Basic demo usage for a node.js client.
1+
// Basic demo usage for openai's node.js client.
22
// Run with:
3-
// yarn ts-node samples/sample-usage.ts
3+
// node example.js
44
import * as dotenv from 'dotenv';
55
import { Configuration, OpenAIApi } from 'openai';
66

examples/node-js/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "node-js-example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"type": "module",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"dotenv": "^16.0.3",
15+
"openai": "^3.1.0"
16+
}
17+
}

examples/node-js/yarn.lock

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
asynckit@^0.4.0:
6+
version "0.4.0"
7+
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
8+
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
9+
10+
axios@^0.26.0:
11+
version "0.26.1"
12+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
13+
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
14+
dependencies:
15+
follow-redirects "^1.14.8"
16+
17+
combined-stream@^1.0.8:
18+
version "1.0.8"
19+
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
20+
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
21+
dependencies:
22+
delayed-stream "~1.0.0"
23+
24+
delayed-stream@~1.0.0:
25+
version "1.0.0"
26+
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
27+
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==
28+
29+
dotenv@^16.0.3:
30+
version "16.0.3"
31+
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07"
32+
integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==
33+
34+
follow-redirects@^1.14.8:
35+
version "1.15.2"
36+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
37+
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
38+
39+
form-data@^4.0.0:
40+
version "4.0.0"
41+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
42+
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
43+
dependencies:
44+
asynckit "^0.4.0"
45+
combined-stream "^1.0.8"
46+
mime-types "^2.1.12"
47+
48+
49+
version "1.52.0"
50+
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
51+
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
52+
53+
mime-types@^2.1.12:
54+
version "2.1.35"
55+
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
56+
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
57+
dependencies:
58+
mime-db "1.52.0"
59+
60+
openai@^3.1.0:
61+
version "3.1.0"
62+
resolved "https://registry.yarnpkg.com/openai/-/openai-3.1.0.tgz#13bfb228cf777155b882c2deb3a03bc5094cb7b3"
63+
integrity sha512-v5kKFH5o+8ld+t0arudj833Mgm3GcgBnbyN9946bj6u7bvel4Yg6YFz2A4HLIYDzmMjIo0s6vSG9x73kOwvdCg==
64+
dependencies:
65+
axios "^0.26.0"
66+
form-data "^4.0.0"

examples/python/.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Create a file called ".env" and add this env var:
2+
OPENAI_API_KEY=...insertme...

examples/python/.python-version

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

examples/python/example.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Basic demo usage for openai's python client.
2+
# Run with:
3+
# poetry run python example.py
4+
import os
5+
from dotenv import load_dotenv
6+
import openai
7+
8+
load_dotenv() # take environment variables from .env.
9+
10+
api_key = os.getenv('OPENAI_API_KEY')
11+
if not api_key:
12+
print('No OPENAI_API_KEY env var found. Please set one in `.env`')
13+
exit(1)
14+
15+
openai.api_key = api_key
16+
17+
# Set this to your local instance or Cloudflare Deployment:
18+
openai.api_base = "http://localhost:8787/proxy"
19+
20+
def make_sample_requests():
21+
print('Making sample requests')
22+
completion1 = openai.Completion.create(
23+
model="text-ada-001",
24+
prompt="write a poem about computers"
25+
)
26+
print('Completion 1:')
27+
print(completion1)
28+
29+
# Sample prompt but different options (different cache)
30+
completion2 = openai.Completion.create(
31+
model="text-ada-001",
32+
prompt="write a poem about computers",
33+
max_tokens=50
34+
)
35+
print('Completion 2:')
36+
print(completion2)
37+
38+
# Run sample requests twice.
39+
# First set will not be cached. Second set will return cached results:
40+
make_sample_requests()
41+
make_sample_requests()

0 commit comments

Comments
 (0)