Skip to content

Commit

Permalink
lanchain code
Browse files Browse the repository at this point in the history
  • Loading branch information
ppant committed Jul 16, 2024
1 parent e0e0fc7 commit c9bc4ef
Show file tree
Hide file tree
Showing 12 changed files with 5,136 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TOGETHER_API_KEY="2ba0e7195028e71675f4ac4fa3d2627f6e5f91e35c107402ca1833b262d7c964"
233 changes: 233 additions & 0 deletions .ipynb_checkpoints/together_ai_service_setup-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d14929df",
"metadata": {},
"outputs": [],
"source": [
"# Set up environment if you saved the API key in a .env file\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv())"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5af5eec2",
"metadata": {},
"outputs": [],
"source": [
"# define the together.ai API url\n",
"url = \"https://api.together.xyz/inference\""
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "e4855676",
"metadata": {},
"outputs": [],
"source": [
"# Set up the together.ai API key -- we'll pick key from .env folder\n",
"import os\n",
"together_api_key = os.getenv('TOGETHER_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "e8ad5d82",
"metadata": {},
"outputs": [],
"source": [
"together_api_key\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9e59cf7b",
"metadata": {},
"outputs": [],
"source": [
"# Store keywords that will be passed to the API\n",
"headers = {\n",
" \"Authorization\": f\"Bearer {together_api_key}\",\n",
" \"Content-Type\": \"application/json\"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "42830fc5",
"metadata": {},
"outputs": [],
"source": [
"# Choose the model to call\n",
"model=\"togethercomputer/llama-2-7b-chat\""
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "580aa289",
"metadata": {},
"outputs": [],
"source": [
"prompt = \"\"\"\n",
"Please write me a birthday card for my wife.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "df49a943",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[INST]\n",
"Please write me a birthday card for my wife.\n",
"[/INST]\n"
]
}
],
"source": [
"# Add instruction tags to the prompt\n",
"prompt = f\"[INST]{prompt}[/INST]\"\n",
"print(prompt)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "4e8fb5db",
"metadata": {},
"outputs": [],
"source": [
"# Set temperature and max_tokens\n",
"temperature = 0.0\n",
"max_tokens = 1024"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "7b62f02e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'model': 'togethercomputer/llama-2-7b-chat',\n",
" 'prompt': '[INST]\\nPlease write me a birthday card for my wife.\\n[/INST]',\n",
" 'temperature': 0.0,\n",
" 'max_tokens': 1024}"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {\n",
" \"model\": model,\n",
" \"prompt\": prompt,\n",
" \"temperature\": temperature,\n",
" \"max_tokens\": max_tokens\n",
"}\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "6955e717",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"response = requests.post(url,\n",
" headers=headers,\n",
" json=data)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "5cfff403",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "ea8caf30",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" Of course, I'd be happy to help you write a birthday card for your wife! Here's a suggestion:\\n\\nDear [Wife's Name],\\n\\nHappy birthday to the most amazing woman in my life! Today is all about celebrating you and all the love, joy, and happiness you bring to our relationship.\\n\\nYou are the sunshine that brightens up my day, the calm in every storm, and the safe haven where I can always find peace. You are my partner, my friend, and my everything.\\n\\nAs you celebrate another year of life, I hope you know how much I appreciate you and all that you do. You deserve all the happiness in the world, and I'm so grateful to be by your side to share it with you.\\n\\nHere's to another year of love, laughter, and adventures together! I love you more than words can express.\\n\\nHappy birthday, my darling!\\n\\nWith all my love,\\n[Your Name]\""
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.json()['output']['choices'][0]['text']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "144efd81",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit c9bc4ef

Please sign in to comment.