summaryrefslogtreecommitdiff
path: root/llm
diff options
context:
space:
mode:
Diffstat (limited to 'llm')
-rw-r--r--llm/tutorials/01_openai_api.ipynb445
1 files changed, 445 insertions, 0 deletions
diff --git a/llm/tutorials/01_openai_api.ipynb b/llm/tutorials/01_openai_api.ipynb
new file mode 100644
index 0000000..38de0fc
--- /dev/null
+++ b/llm/tutorials/01_openai_api.ipynb
@@ -0,0 +1,445 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T14:55:17.764476Z",
+ "start_time": "2023-02-27T14:55:17.761677Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "import openai"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T14:56:33.903156Z",
+ "start_time": "2023-02-27T14:56:33.898811Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "openai.api_key = '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'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## openai.Completion.create"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "- engine => model\n",
+ " - engine\n",
+ " - ada:350M params, fast\n",
+ " - babbge:1.3B\n",
+ " - curie:6.7B\n",
+ " - davinci:175B,most powerful;\n",
+ "- 注意看标点符号;"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 32,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T14:58:56.780716Z",
+ "start_time": "2023-02-27T14:58:53.940821Z"
+ },
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<OpenAIObject text_completion id=cmpl-6oZFntROV0vq5CpYsy207UFhZ4bSv at 0x12a16c0f8> JSON: {\n",
+ " \"choices\": [\n",
+ " {\n",
+ " \"finish_reason\": \"length\",\n",
+ " \"index\": 0,\n",
+ " \"logprobs\": null,\n",
+ " \"text\": \"\\\");\\n });\\n\\n it('AddText#can get text content with\"\n",
+ " }\n",
+ " ],\n",
+ " \"created\": 1677509935,\n",
+ " \"id\": \"cmpl-6oZFntROV0vq5CpYsy207UFhZ4bSv\",\n",
+ " \"model\": \"text-davinci-002\",\n",
+ " \"object\": \"text_completion\",\n",
+ " \"usage\": {\n",
+ " \"completion_tokens\": 16,\n",
+ " \"prompt_tokens\": 5,\n",
+ " \"total_tokens\": 21\n",
+ " }\n",
+ "}"
+ ]
+ },
+ "execution_count": 32,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "openai.Completion.create(model=\"text-davinci-002\", prompt=\"Say this is a test\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Say this is a test\n",
+ "\n",
+ "\n",
+ "This is indeed a test."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T14:59:15.828861Z",
+ "start_time": "2023-02-27T14:59:12.705447Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<OpenAIObject text_completion id=cmpl-6oZG5UcSCWJ8KiOyisfxlY28tstvX at 0x12a16c4c0> JSON: {\n",
+ " \"choices\": [\n",
+ " {\n",
+ " \"finish_reason\": null,\n",
+ " \"index\": 0,\n",
+ " \"logprobs\": null,\n",
+ " \"text\": \"\\n\\nThis is indeed a test.\"\n",
+ " }\n",
+ " ],\n",
+ " \"created\": 1677509953,\n",
+ " \"id\": \"cmpl-6oZG5UcSCWJ8KiOyisfxlY28tstvX\",\n",
+ " \"model\": \"text-davinci-003\",\n",
+ " \"object\": \"text_completion\",\n",
+ " \"usage\": {\n",
+ " \"completion_tokens\": 8,\n",
+ " \"prompt_tokens\": 5,\n",
+ " \"total_tokens\": 13\n",
+ " }\n",
+ "}"
+ ]
+ },
+ "execution_count": 33,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "openai.Completion.create(model=\"text-davinci-003\", prompt=\"Say this is a test\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T15:00:03.335078Z",
+ "start_time": "2023-02-27T15:00:01.731030Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<OpenAIObject text_completion id=cmpl-6oZGsgvsVprUhx0OpA4MCW44szBya at 0x12a154d00> JSON: {\n",
+ " \"choices\": [\n",
+ " {\n",
+ " \"finish_reason\": \"length\",\n",
+ " \"index\": 0,\n",
+ " \"logprobs\": null,\n",
+ " \"text\": \", there was a young girl named Anastasia. Anastasia was a\"\n",
+ " }\n",
+ " ],\n",
+ " \"created\": 1677510002,\n",
+ " \"id\": \"cmpl-6oZGsgvsVprUhx0OpA4MCW44szBya\",\n",
+ " \"model\": \"text-davinci-003\",\n",
+ " \"object\": \"text_completion\",\n",
+ " \"usage\": {\n",
+ " \"completion_tokens\": 16,\n",
+ " \"prompt_tokens\": 4,\n",
+ " \"total_tokens\": 20\n",
+ " }\n",
+ "}"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "openai.Completion.create(model=\"text-davinci-003\", prompt=\"Once upon a time\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T15:01:58.866141Z",
+ "start_time": "2023-02-27T15:01:46.228840Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<OpenAIObject text_completion id=cmpl-6oZIYbYLMrUEawSmh1zvu5Ciy80e5 at 0x12a738780> JSON: {\n",
+ " \"choices\": [\n",
+ " {\n",
+ " \"finish_reason\": \"stop\",\n",
+ " \"index\": 0,\n",
+ " \"logprobs\": null,\n",
+ " \"text\": \"\\n\\nthere was a beautiful princess who lived in a magical kingdom. She was kind and generous, and she had a lot of admirers, both human and magical.\\n\\nOne day, a dark force appeared in the kingdom, and the princess was worried. She set out to find the source of the evil and put an end to it.\\n\\nShe traveled through forests, across rivers, and up mountains. Along the way, she met strange creatures and made new allies. Finally, after weeks of searching, she reached the peak of a high mountain and discovered the source of the evil -- a powerful sorcerer!\\n\\nThe princess bravely confronted the sorcerer and asked him to leave. The sorcerer refused, and a battle ensued. The princess used all of her magical skills to outwit the sorcerer, and eventually he was vanquished.\\n\\nThe kingdom was safe once more, and the princess was hailed as a hero. All of the creatures that she had befriended along the way celebrated her bravery and thanked her for saving their kingdom.\\n\\nThe princess returned to the castle, ready to enjoy a long and peaceful reign.\"\n",
+ " }\n",
+ " ],\n",
+ " \"created\": 1677510106,\n",
+ " \"id\": \"cmpl-6oZIYbYLMrUEawSmh1zvu5Ciy80e5\",\n",
+ " \"model\": \"text-davinci-003\",\n",
+ " \"object\": \"text_completion\",\n",
+ " \"usage\": {\n",
+ " \"completion_tokens\": 227,\n",
+ " \"prompt_tokens\": 4,\n",
+ " \"total_tokens\": 231\n",
+ " }\n",
+ "}"
+ ]
+ },
+ "execution_count": 36,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "openai.Completion.create(model=\"text-davinci-003\", prompt=\"Once upon a time\", max_tokens=256)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 37,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T15:02:44.314526Z",
+ "start_time": "2023-02-27T15:02:40.954387Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<OpenAIObject text_completion id=cmpl-6oZJROYB08l97UNvGeKEILgHRciyy at 0x12a7386d0> JSON: {\n",
+ " \"choices\": [\n",
+ " {\n",
+ " \"finish_reason\": \"stop\",\n",
+ " \"index\": 0,\n",
+ " \"logprobs\": null,\n",
+ " \"text\": \"\\n\\nThe moon landing was when people from Earth explored the moon for the first time. They flew a special spaceship to the moon, and then people got out and walked on the moon! They explored and collected rocks from the moon, and then came back to Earth.\"\n",
+ " }\n",
+ " ],\n",
+ " \"created\": 1677510161,\n",
+ " \"id\": \"cmpl-6oZJROYB08l97UNvGeKEILgHRciyy\",\n",
+ " \"model\": \"text-davinci-003\",\n",
+ " \"object\": \"text_completion\",\n",
+ " \"usage\": {\n",
+ " \"completion_tokens\": 54,\n",
+ " \"prompt_tokens\": 15,\n",
+ " \"total_tokens\": 69\n",
+ " }\n",
+ "}"
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "openai.Completion.create(model=\"text-davinci-003\", \n",
+ " prompt=\"Explain the moon landing to a 6 year old in a few sentences.\", max_tokens=512)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### tasks"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T15:04:12.599004Z",
+ "start_time": "2023-02-27T15:04:09.906722Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<OpenAIObject text_completion id=cmpl-6oZKtdrPmiXEAlQTK7DDooYZqdgyI at 0x12a7383b8> JSON: {\n",
+ " \"choices\": [\n",
+ " {\n",
+ " \"finish_reason\": \"stop\",\n",
+ " \"index\": 0,\n",
+ " \"logprobs\": null,\n",
+ " \"text\": \"\\n\\nShe did not go to the market.\"\n",
+ " }\n",
+ " ],\n",
+ " \"created\": 1677510251,\n",
+ " \"id\": \"cmpl-6oZKtdrPmiXEAlQTK7DDooYZqdgyI\",\n",
+ " \"model\": \"text-davinci-003\",\n",
+ " \"object\": \"text_completion\",\n",
+ " \"usage\": {\n",
+ " \"completion_tokens\": 10,\n",
+ " \"prompt_tokens\": 15,\n",
+ " \"total_tokens\": 25\n",
+ " }\n",
+ "}"
+ ]
+ },
+ "execution_count": 38,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "openai.Completion.create(model=\"text-davinci-003\", \n",
+ " prompt=\"Correct this to standard English:\\n\\nShe no went to the market.\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## openai.Image.create"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 41,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T15:05:42.135448Z",
+ "start_time": "2023-02-27T15:05:42.132668Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "prompt = 'a photograph of an astronaut riding a horse'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T15:05:49.140402Z",
+ "start_time": "2023-02-27T15:05:43.188866Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "\n",
+ "generation_response = openai.Image.create(\n",
+ " prompt=prompt,\n",
+ " n=1,\n",
+ " size=\"1024x1024\",\n",
+ " response_format=\"url\",\n",
+ ")\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 43,
+ "metadata": {
+ "ExecuteTime": {
+ "end_time": "2023-02-27T15:05:50.359537Z",
+ "start_time": "2023-02-27T15:05:50.354815Z"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "<OpenAIObject at 0x12a16c678> JSON: {\n",
+ " \"created\": 1677510348,\n",
+ " \"data\": [\n",
+ " {\n",
+ " \"url\": \"https://oaidalleapiprodscus.blob.core.windows.net/private/org-SiC9BnWmtIrrvb4buhYLx3LA/user-GswrqPcaLILHe9R1JbfuOFmD/img-cRInhoHKRTJt0hRK4mtTeQqR.png?st=2023-02-27T14%3A05%3A48Z&se=2023-02-27T16%3A05%3A48Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-02-27T09%3A35%3A43Z&ske=2023-02-28T09%3A35%3A43Z&sks=b&skv=2021-08-06&sig=J5oExlCiahr6ItdOmH97iLH%2B4VZoFiP8LPql%2B0dugGU%3D\"\n",
+ " }\n",
+ " ]\n",
+ "}"
+ ]
+ },
+ "execution_count": 43,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "generation_response"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.7.3"
+ },
+ "toc": {
+ "base_numbering": 1,
+ "nav_menu": {},
+ "number_sections": true,
+ "sideBar": true,
+ "skip_h1_title": false,
+ "title_cell": "Table of Contents",
+ "title_sidebar": "Contents",
+ "toc_cell": false,
+ "toc_position": {},
+ "toc_section_display": true,
+ "toc_window_display": true
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+}