-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dphuang2
committed
Apr 12, 2024
1 parent
79913b4
commit 8e02e9c
Showing
10 changed files
with
14,429 additions
and
0 deletions.
There are no files selected for viewing
431 changes: 431 additions & 0 deletions
431
misc/building-ai-applications/.ipynb_checkpoints/Chatbots-checkpoint.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
230 changes: 230 additions & 0 deletions
230
...ications/.ipynb_checkpoints/Generating Guides from OpenAPI Specification-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "98cbe3b5-c41b-437a-a888-ef79da152dc0", | ||
"metadata": {}, | ||
"source": [ | ||
"### Generating Guides from OpenAPI Specification" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e2a11448", | ||
"metadata": {}, | ||
"source": [ | ||
"### (1) Rudimentary Example\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"```mermaid\n", | ||
"graph LR\n", | ||
" allow.com.yaml --> DocumentLoader\n", | ||
" DocumentLoader --> Chat\n", | ||
" Query[How do I create a journey in Python?] --> Chat\n", | ||
" Chat --> Guide\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "6cbdeb3a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_openai import ChatOpenAI\n", | ||
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", | ||
"from langchain.chains.combine_documents import create_stuff_documents_chain\n", | ||
"\n", | ||
"\n", | ||
"chat = ChatOpenAI(model=\"gpt-4-turbo-preview\", temperature=0.2)\n", | ||
"question_answering_prompt = ChatPromptTemplate.from_messages(\n", | ||
" [\n", | ||
" (\n", | ||
" \"system\",\n", | ||
" \"Answer the user's questions based on the below context:\\n\\n{context}\",\n", | ||
" ),\n", | ||
" MessagesPlaceholder(variable_name=\"messages\"),\n", | ||
" ]\n", | ||
")\n", | ||
"document_chain = create_stuff_documents_chain(chat, question_answering_prompt)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"id": "0a81101f-7a0e-4222-a088-0a3fd09a564f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain.document_loaders.text import TextLoader\n", | ||
"\n", | ||
"file_path = 'alloy.com.yaml'\n", | ||
"loader = TextLoader(file_path)\n", | ||
"docs = loader.load()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 16, | ||
"id": "8a4efa9c-c3d2-4dbe-85b6-63f38db462cf", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'To create a journey in Python using the Alloy API, you would typically perform a POST request to the `/journeys/{journey_token}/applications` endpoint. This request would create a journey application for one or more entities based on the journey specified by the `journey_token`. Below is an example of how you might do this using Python\\'s `requests` library.\\n\\nFirst, ensure you have the `requests` library installed. If not, you can install it using pip:\\n\\n```bash\\npip install requests\\n```\\n\\nHere\\'s a sample Python script to create a journey application:\\n\\n```python\\nimport requests\\nimport json\\n\\n# Replace these variables with your actual data\\njourney_token = \"your_journey_token_here\"\\nauthorization_header = \"Basic your_base64_encoded_credentials\"\\nalloy_api_url = f\"https://demo-qasandbox.alloy.co/v1/journeys/{journey_token}/applications\"\\n\\n# Sample payload. Replace or modify the entities as per your journey\\'s requirements\\npayload = {\\n \"entities\": [\\n {\\n \"external_entity_id\": \"person-entity\",\\n \"gender\": \"male\",\\n \"birth_date\": \"1998-02-03\",\\n \"name_first\": \"John\",\\n \"name_last\": \"Doe\",\\n \"document_ssn\": \"123456789\",\\n \"addresses\": [\\n {\\n \"address_line_1\": \"41 E. 11th\",\\n \"address_city\": \"New York\",\\n \"address_state\": \"NY\",\\n \"address_postal_code\": \"10003\",\\n \"address_country_code\": \"US\",\\n \"type\": \"primary\"\\n }\\n ],\\n \"emails\": [\\n {\\n \"email_address\": \"[email protected]\"\\n }\\n ],\\n \"phones\": [\\n {\\n \"phone_number\": \"555-000-1234\"\\n }\\n ],\\n \"pii_status\": \"successful\"\\n }\\n ]\\n}\\n\\nheaders = {\\n \"Authorization\": authorization_header,\\n \"Content-Type\": \"application/json\"\\n}\\n\\nresponse = requests.post(alloy_api_url, headers=headers, data=json.dumps(payload))\\n\\nif response.status_code == 201:\\n print(\"Journey application created successfully.\")\\n print(response.json())\\nelse:\\n print(f\"Failed to create journey application. Status code: {response.status_code}\")\\n print(response.json())\\n```\\n\\nThis script constructs a POST request to the Alloy API to create a journey application. It includes a sample payload with one entity. You\\'ll need to replace `your_journey_token_here` with your actual journey token and `your_base64_encoded_credentials` with your Base64-encoded `application_token:application_secret`. \\n\\nThe `external_entity_id`, `gender`, `birth_date`, `name_first`, `name_last`, `document_ssn`, `addresses`, `emails`, `phones`, and `pii_status` in the payload should be adjusted according to the specific requirements of your journey and the data you have.\\n\\nAfter sending the request, the script checks the response status code. If the journey application is created successfully, it prints the success message and the response JSON. Otherwise, it prints an error message along with the status code and response JSON for debugging.'" | ||
] | ||
}, | ||
"execution_count": 16, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from langchain.memory import ChatMessageHistory\n", | ||
"\n", | ||
"demo_ephemeral_chat_history = ChatMessageHistory()\n", | ||
"\n", | ||
"demo_ephemeral_chat_history.add_user_message(\"How do I create a journey in Python?\")\n", | ||
"\n", | ||
"document_chain.invoke(\n", | ||
" {\n", | ||
" \"messages\": demo_ephemeral_chat_history.messages,\n", | ||
" \"context\": docs,\n", | ||
" }\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"id": "7791d6df-c1f1-43ea-aa57-6aff4dc3bb73", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"output = 'To create a journey in Python using the Alloy API, you would typically perform a POST request to the `/journeys/{journey_token}/applications` endpoint. This request would create a journey application for one or more entities based on the journey specified by the `journey_token`. Below is an example of how you might do this using Python\\'s `requests` library.\\n\\nFirst, ensure you have the `requests` library installed. If not, you can install it using pip:\\n\\n```bash\\npip install requests\\n```\\n\\nHere\\'s a sample Python script to create a journey application:\\n\\n```python\\nimport requests\\nimport json\\n\\n# Replace these variables with your actual data\\njourney_token = \"your_journey_token_here\"\\nauthorization_header = \"Basic your_base64_encoded_credentials\"\\nalloy_api_url = f\"https://demo-qasandbox.alloy.co/v1/journeys/{journey_token}/applications\"\\n\\n# Sample payload. Replace or modify the entities as per your journey\\'s requirements\\npayload = {\\n \"entities\": [\\n {\\n \"external_entity_id\": \"person-entity\",\\n \"gender\": \"male\",\\n \"birth_date\": \"1998-02-03\",\\n \"name_first\": \"John\",\\n \"name_last\": \"Doe\",\\n \"document_ssn\": \"123456789\",\\n \"addresses\": [\\n {\\n \"address_line_1\": \"41 E. 11th\",\\n \"address_city\": \"New York\",\\n \"address_state\": \"NY\",\\n \"address_postal_code\": \"10003\",\\n \"address_country_code\": \"US\",\\n \"type\": \"primary\"\\n }\\n ],\\n \"emails\": [\\n {\\n \"email_address\": \"[email protected]\"\\n }\\n ],\\n \"phones\": [\\n {\\n \"phone_number\": \"555-000-1234\"\\n }\\n ],\\n \"pii_status\": \"successful\"\\n }\\n ]\\n}\\n\\nheaders = {\\n \"Authorization\": authorization_header,\\n \"Content-Type\": \"application/json\"\\n}\\n\\nresponse = requests.post(alloy_api_url, headers=headers, data=json.dumps(payload))\\n\\nif response.status_code == 201:\\n print(\"Journey application created successfully.\")\\n print(response.json())\\nelse:\\n print(f\"Failed to create journey application. Status code: {response.status_code}\")\\n print(response.json())\\n```\\n\\nThis script constructs a POST request to the Alloy API to create a journey application. It includes a sample payload with one entity. You\\'ll need to replace `your_journey_token_here` with your actual journey token and `your_base64_encoded_credentials` with your Base64-encoded `application_token:application_secret`. \\n\\nThe `external_entity_id`, `gender`, `birth_date`, `name_first`, `name_last`, `document_ssn`, `addresses`, `emails`, `phones`, and `pii_status` in the payload should be adjusted according to the specific requirements of your journey and the data you have.\\n\\nAfter sending the request, the script checks the response status code. If the journey application is created successfully, it prints the success message and the response JSON. Otherwise, it prints an error message along with the status code and response JSON for debugging.'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"id": "0187cf3f-bc8e-49c7-9acc-f412b0e57ed2", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/markdown": [ | ||
"To create a journey in Python using the Alloy API, you would typically perform a POST request to the `/journeys/{journey_token}/applications` endpoint. This request would create a journey application for one or more entities based on the journey specified by the `journey_token`. Below is an example of how you might do this using Python's `requests` library.\n", | ||
"\n", | ||
"First, ensure you have the `requests` library installed. If not, you can install it using pip:\n", | ||
"\n", | ||
"```bash\n", | ||
"pip install requests\n", | ||
"```\n", | ||
"\n", | ||
"Here's a sample Python script to create a journey application:\n", | ||
"\n", | ||
"```python\n", | ||
"import requests\n", | ||
"import json\n", | ||
"\n", | ||
"# Replace these variables with your actual data\n", | ||
"journey_token = \"your_journey_token_here\"\n", | ||
"authorization_header = \"Basic your_base64_encoded_credentials\"\n", | ||
"alloy_api_url = f\"https://demo-qasandbox.alloy.co/v1/journeys/{journey_token}/applications\"\n", | ||
"\n", | ||
"# Sample payload. Replace or modify the entities as per your journey's requirements\n", | ||
"payload = {\n", | ||
" \"entities\": [\n", | ||
" {\n", | ||
" \"external_entity_id\": \"person-entity\",\n", | ||
" \"gender\": \"male\",\n", | ||
" \"birth_date\": \"1998-02-03\",\n", | ||
" \"name_first\": \"John\",\n", | ||
" \"name_last\": \"Doe\",\n", | ||
" \"document_ssn\": \"123456789\",\n", | ||
" \"addresses\": [\n", | ||
" {\n", | ||
" \"address_line_1\": \"41 E. 11th\",\n", | ||
" \"address_city\": \"New York\",\n", | ||
" \"address_state\": \"NY\",\n", | ||
" \"address_postal_code\": \"10003\",\n", | ||
" \"address_country_code\": \"US\",\n", | ||
" \"type\": \"primary\"\n", | ||
" }\n", | ||
" ],\n", | ||
" \"emails\": [\n", | ||
" {\n", | ||
" \"email_address\": \"[email protected]\"\n", | ||
" }\n", | ||
" ],\n", | ||
" \"phones\": [\n", | ||
" {\n", | ||
" \"phone_number\": \"555-000-1234\"\n", | ||
" }\n", | ||
" ],\n", | ||
" \"pii_status\": \"successful\"\n", | ||
" }\n", | ||
" ]\n", | ||
"}\n", | ||
"\n", | ||
"headers = {\n", | ||
" \"Authorization\": authorization_header,\n", | ||
" \"Content-Type\": \"application/json\"\n", | ||
"}\n", | ||
"\n", | ||
"response = requests.post(alloy_api_url, headers=headers, data=json.dumps(payload))\n", | ||
"\n", | ||
"if response.status_code == 201:\n", | ||
" print(\"Journey application created successfully.\")\n", | ||
" print(response.json())\n", | ||
"else:\n", | ||
" print(f\"Failed to create journey application. Status code: {response.status_code}\")\n", | ||
" print(response.json())\n", | ||
"```\n", | ||
"\n", | ||
"This script constructs a POST request to the Alloy API to create a journey application. It includes a sample payload with one entity. You'll need to replace `your_journey_token_here` with your actual journey token and `your_base64_encoded_credentials` with your Base64-encoded `application_token:application_secret`. \n", | ||
"\n", | ||
"The `external_entity_id`, `gender`, `birth_date`, `name_first`, `name_last`, `document_ssn`, `addresses`, `emails`, `phones`, and `pii_status` in the payload should be adjusted according to the specific requirements of your journey and the data you have.\n", | ||
"\n", | ||
"After sending the request, the script checks the response status code. If the journey application is created successfully, it prints the success message and the response JSON. Otherwise, it prints an error message along with the status code and response JSON for debugging." | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.Markdown object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"from IPython.display import display, Markdown\n", | ||
"\n", | ||
"display(Markdown(output))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
117 changes: 117 additions & 0 deletions
117
misc/building-ai-applications/.ipynb_checkpoints/RAG-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "edc14b04-7e1c-41c2-92a5-4a4c0017924a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"import bs4\n", | ||
"from langchain import hub\n", | ||
"from langchain_community.document_loaders import WebBaseLoader\n", | ||
"from langchain_community.vectorstores import Chroma\n", | ||
"from langchain_core.output_parsers import StrOutputParser\n", | ||
"from langchain_core.runnables import RunnablePassthrough\n", | ||
"from langchain_openai import OpenAIEmbeddings\n", | ||
"from langchain_text_splitters import RecursiveCharacterTextSplitter" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "1473caab-04ca-4fa1-b4eb-24ffbafe4c44", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from langchain_openai import ChatOpenAI\n", | ||
"\n", | ||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "ccbac0f7-d2de-4c0a-b45b-1d93bf5f3f68", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Load, chunk and index the contents of the blog.\n", | ||
"loader = WebBaseLoader(\n", | ||
" web_paths=(\"https://docs.snaptrade.com/demo/getting-started\",),\n", | ||
")\n", | ||
"docs = loader.load()\n", | ||
"\n", | ||
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n", | ||
"splits = text_splitter.split_documents(docs)\n", | ||
"vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())\n", | ||
"\n", | ||
"# Retrieve and generate using the relevant snippets of the blog.\n", | ||
"retriever = vectorstore.as_retriever()\n", | ||
"prompt = hub.pull(\"rlm/rag-prompt\")\n", | ||
"\n", | ||
"def format_docs(docs):\n", | ||
" return \"\\n\\n\".join(doc.page_content for doc in docs)\n", | ||
"\n", | ||
"\n", | ||
"rag_chain = (\n", | ||
" {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n", | ||
" | prompt\n", | ||
" | llm\n", | ||
" | StrOutputParser()\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"id": "ad94e048-289f-444e-9552-a3e41bcb9be4", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'To get started with SnapTrade, you need to initialize a client with your clientId and consumerKey, which can be obtained by contacting [email protected]. Check that the client is able to make a request to the API server by calling the API Status endpoint. Create a new user on SnapTrade by calling the Register User endpoint with a userId to receive a user id and user secret for accessing user data.'" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"rag_chain.invoke(\"How do I get started with SnapTrade?\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b7515f7d-496c-423d-812b-08a9c17e7253", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```mermaid | ||
graph TD; | ||
A[Start] --> B{Is it?}; | ||
B -->|Yes| C[End]; | ||
C -->|No| D[End]; | ||
B -->|No| D[End]; | ||
``` |
Oops, something went wrong.