|
1 | 1 | { |
2 | | - "cells": [ |
3 | | - { |
4 | | - "cell_type": "markdown", |
5 | | - "metadata": {}, |
6 | | - "source": [ |
7 | | - "## Installation\n", |
8 | | - "\n", |
9 | | - "Use pip to install the Commonbase Python SDK" |
10 | | - ] |
11 | | - }, |
12 | | - { |
13 | | - "cell_type": "code", |
14 | | - "execution_count": null, |
15 | | - "metadata": { |
16 | | - "vscode": { |
17 | | - "languageId": "shellscript" |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Installation\n", |
| 8 | + "\n", |
| 9 | + "Use pip to install the Commonbase Python SDK" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": { |
| 16 | + "vscode": { |
| 17 | + "languageId": "shellscript" |
| 18 | + } |
| 19 | + }, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "pip install commonbase" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "metadata": {}, |
| 28 | + "source": [ |
| 29 | + "## Usage" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "A Project ID and API Key are required for all Commonbase requests. You can find your project ID and generate an API key in the [Commonbase Dashboard](https://commonbase.com/).\n", |
| 37 | + "\n", |
| 38 | + "To create a completion, provide your Project ID, API Key, and prompt to `Completion.create`." |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "code", |
| 43 | + "execution_count": null, |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "import commonbase\n", |
| 48 | + "\n", |
| 49 | + "result = commonbase.Completion.create(\n", |
| 50 | + " api_key=\"API_KEY\",\n", |
| 51 | + " project_id=\"PROJECT_ID\", \n", |
| 52 | + " prompt=\"Hello!\"\n", |
| 53 | + ")\n", |
| 54 | + "\n", |
| 55 | + "print(result.best_result)" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "markdown", |
| 60 | + "metadata": {}, |
| 61 | + "source": [ |
| 62 | + "Use `Completion.stream` to stream a completion response." |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": null, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "import commonbase\n", |
| 72 | + "\n", |
| 73 | + "result = commonbase.Completion.stream(\n", |
| 74 | + " api_key=\"API_KEY\",\n", |
| 75 | + " project_id=\"PROJECT_ID\", \n", |
| 76 | + " prompt=\"Write me a short essay about artificial intelligence.\"\n", |
| 77 | + ")\n", |
| 78 | + "\n", |
| 79 | + "for completion in result:\n", |
| 80 | + " if not completion.completed:\n", |
| 81 | + " print(completion.best_result, end=\"\")\n", |
| 82 | + " else:\n", |
| 83 | + " # The final response from the stream contains the full completion text.\n", |
| 84 | + " # We can ignore it here because we already printed the intermediate results.\n", |
| 85 | + " print(\"\\n\\ndone\")" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "### Variables\n", |
| 93 | + "\n", |
| 94 | + "You can also create a completion by providing variables for your managed prompt on the [Commonbase Dashboard](https://commonbase.com/)." |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "import commonbase\n", |
| 104 | + "\n", |
| 105 | + "result = commonbase.Completion.create(\n", |
| 106 | + " api_key=\"API_KEY\",\n", |
| 107 | + " project_id=\"PROJECT_ID\", \n", |
| 108 | + " variables={\n", |
| 109 | + " \"user_name\": \"USERNAME\",\n", |
| 110 | + " \"email\": \"USERNAME@COMPANY.COM\"\n", |
| 111 | + " }\n", |
| 112 | + ")\n", |
| 113 | + "\n", |
| 114 | + "print(result.best_result)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "### Chat\n", |
| 122 | + "\n", |
| 123 | + "To create a chat completion, use the `chat_context` argument to provide a list of chat messages.\n", |
| 124 | + "You must also set the OpenAI configuration to `chat`. In this mode, the `prompt` argument\n", |
| 125 | + "functions as a system message." |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "import commonbase\n", |
| 135 | + "\n", |
| 136 | + "result = commonbase.Completion.create(\n", |
| 137 | + " api_key=\"API_KEY\",\n", |
| 138 | + " project_id=\"PROJECT_ID\", \n", |
| 139 | + " prompt=\"You are an assistant who helps users with tech problems.\",\n", |
| 140 | + " chat_context=commonbase.ChatContext([\n", |
| 141 | + " commonbase.ChatMessage(role=\"user\", content=\"My internet isn't working.\"),\n", |
| 142 | + " commonbase.ChatMessage(role=\"assistant\", content=\"Have you tried restarting your router?\"),\n", |
| 143 | + " commonbase.ChatMessage(role=\"user\", content=\"Yes I've tried that.\"),\n", |
| 144 | + " ]),\n", |
| 145 | + " provider_config=commonbase.ProviderConfig(\n", |
| 146 | + " provider=\"cb-openai-eu\", params=commonbase.OpenAIParams(type=\"chat\")\n", |
| 147 | + " ),\n", |
| 148 | + ")\n", |
| 149 | + "\n", |
| 150 | + "print(result.best_result)" |
| 151 | + ] |
18 | 152 | } |
19 | | - }, |
20 | | - "outputs": [], |
21 | | - "source": [ |
22 | | - "pip install commonbase" |
23 | | - ] |
24 | | - }, |
25 | | - { |
26 | | - "cell_type": "markdown", |
27 | | - "metadata": {}, |
28 | | - "source": [ |
29 | | - "## Usage" |
30 | | - ] |
31 | | - }, |
32 | | - { |
33 | | - "cell_type": "markdown", |
34 | | - "metadata": {}, |
35 | | - "source": [ |
36 | | - "A Project ID and API Key are required for all Commonbase requests. You can find your project ID and generate an API key in the [Commonbase Dashboard](https://commonbase.com/).\n", |
37 | | - "\n", |
38 | | - "To create a completion, provide your Project ID, API Key, and prompt to `Completion.create`." |
39 | | - ] |
40 | | - }, |
41 | | - { |
42 | | - "cell_type": "code", |
43 | | - "execution_count": null, |
44 | | - "metadata": {}, |
45 | | - "outputs": [], |
46 | | - "source": [ |
47 | | - "import commonbase\n", |
48 | | - "\n", |
49 | | - "result = commonbase.Completion.create(\n", |
50 | | - " api_key=\"YOUR_API_KEY\",\n", |
51 | | - " project_id=\"YOUR_PROJECT_ID\", \n", |
52 | | - " prompt=\"Hello!\"\n", |
53 | | - ")\n", |
54 | | - "\n", |
55 | | - "print(result.best_result)" |
56 | | - ] |
57 | | - }, |
58 | | - { |
59 | | - "cell_type": "markdown", |
60 | | - "metadata": {}, |
61 | | - "source": [ |
62 | | - "Use `Completion.stream` to stream a completion response." |
63 | | - ] |
64 | | - }, |
65 | | - { |
66 | | - "cell_type": "code", |
67 | | - "execution_count": null, |
68 | | - "metadata": {}, |
69 | | - "outputs": [], |
70 | | - "source": [ |
71 | | - "import commonbase\n", |
72 | | - "\n", |
73 | | - "result = commonbase.Completion.stream(\n", |
74 | | - " api_key=\"YOUR_API_KEY\",\n", |
75 | | - " project_id=\"YOUR_PROJECT_ID\", \n", |
76 | | - " prompt=\"Write me a short essay about artificial intelligence.\"\n", |
77 | | - ")\n", |
78 | | - "\n", |
79 | | - "for completion in result:\n", |
80 | | - " if not completion.completed:\n", |
81 | | - " print(completion.best_result, end=\"\")\n", |
82 | | - " else:\n", |
83 | | - " # The final response from the stream contains the full completion text.\n", |
84 | | - " # We can ignore it here because we already printed the intermediate results.\n", |
85 | | - " print(\"\\n\\ndone\")" |
86 | | - ] |
87 | | - }, |
88 | | - { |
89 | | - "cell_type": "markdown", |
90 | | - "metadata": {}, |
91 | | - "source": [ |
92 | | - "### Variables\n", |
93 | | - "\n", |
94 | | - "You can also create a completion by providing variables for your managed prompt on the [Commonbase Dashboard](https://commonbase.com/)." |
95 | | - ] |
96 | | - }, |
97 | | - { |
98 | | - "cell_type": "code", |
99 | | - "execution_count": null, |
100 | | - "metadata": {}, |
101 | | - "outputs": [], |
102 | | - "source": [ |
103 | | - "import commonbase\n", |
104 | | - "\n", |
105 | | - "result = commonbase.Completion.create(\n", |
106 | | - " api_key=\"YOUR_API_KEY\",\n", |
107 | | - " project_id=\"YOUR_PROJECT_ID\", \n", |
108 | | - " variables={\n", |
109 | | - " \"user_name\": \"USERNAME\",\n", |
110 | | - " \"email\": \"USERNAME@COMPANY.COM\"\n", |
111 | | - " }\n", |
112 | | - ")\n", |
113 | | - "\n", |
114 | | - "print(result.best_result)" |
115 | | - ] |
116 | | - }, |
117 | | - { |
118 | | - "cell_type": "markdown", |
119 | | - "metadata": {}, |
120 | | - "source": [ |
121 | | - "### Chat\n", |
122 | | - "\n", |
123 | | - "To create a chat completion, use the `chat_context` argument to provide a list of chat messages.\n", |
124 | | - "You must also set the OpenAI configuration to `chat`. In this mode, the `prompt` argument\n", |
125 | | - "functions as a system message." |
126 | | - ] |
127 | | - }, |
128 | | - { |
129 | | - "cell_type": "code", |
130 | | - "execution_count": null, |
131 | | - "metadata": {}, |
132 | | - "outputs": [], |
133 | | - "source": [ |
134 | | - "import commonbase\n", |
135 | | - "\n", |
136 | | - "result = commonbase.Completion.create(\n", |
137 | | - " api_key=\"YOUR_API_KEY\",\n", |
138 | | - " project_id=\"YOUR_PROJECT_ID\", \n", |
139 | | - " prompt=\"You are an assistant who helps users with tech problems.\",\n", |
140 | | - " chat_context=commonbase.ChatContext([\n", |
141 | | - " commonbase.ChatMessage(role=\"user\", content=\"My internet isn't working.\"),\n", |
142 | | - " commonbase.ChatMessage(role=\"assistant\", content=\"Have you tried restarting your router?\"),\n", |
143 | | - " commonbase.ChatMessage(role=\"user\", content=\"Yes I've tried that.\"),\n", |
144 | | - " ]),\n", |
145 | | - " provider_config=commonbase.ProviderConfig(\n", |
146 | | - " provider=\"cb-openai-eu\", params=commonbase.OpenAIParams(type=\"chat\")\n", |
147 | | - " ),\n", |
148 | | - ")\n", |
149 | | - "\n", |
150 | | - "print(result.best_result)" |
151 | | - ] |
152 | | - } |
153 | | - ], |
154 | | - "metadata": { |
155 | | - "kernelspec": { |
156 | | - "display_name": "Python 3", |
157 | | - "language": "python", |
158 | | - "name": "python3" |
159 | | - }, |
160 | | - "language_info": { |
161 | | - "codemirror_mode": { |
162 | | - "name": "ipython", |
163 | | - "version": 3 |
164 | | - }, |
165 | | - "file_extension": ".py", |
166 | | - "mimetype": "text/x-python", |
167 | | - "name": "python", |
168 | | - "nbconvert_exporter": "python", |
169 | | - "pygments_lexer": "ipython3", |
170 | | - "version": "3.11.4" |
| 153 | + ], |
| 154 | + "metadata": { |
| 155 | + "kernelspec": { |
| 156 | + "display_name": "Python 3", |
| 157 | + "language": "python", |
| 158 | + "name": "python3" |
| 159 | + }, |
| 160 | + "language_info": { |
| 161 | + "codemirror_mode": { |
| 162 | + "name": "ipython", |
| 163 | + "version": 3 |
| 164 | + }, |
| 165 | + "file_extension": ".py", |
| 166 | + "mimetype": "text/x-python", |
| 167 | + "name": "python", |
| 168 | + "nbconvert_exporter": "python", |
| 169 | + "pygments_lexer": "ipython3", |
| 170 | + "version": "3.11.4" |
| 171 | + }, |
| 172 | + "orig_nbformat": 4 |
171 | 173 | }, |
172 | | - "orig_nbformat": 4 |
173 | | - }, |
174 | | - "nbformat": 4, |
175 | | - "nbformat_minor": 2 |
| 174 | + "nbformat": 4, |
| 175 | + "nbformat_minor": 2 |
176 | 176 | } |
0 commit comments