Skip to content

Instantly share code, notes, and snippets.

@alonsosilvaallende
Created July 16, 2023 21:01
Show Gist options
  • Save alonsosilvaallende/08d039e00d29b0bd97ab220ad78593b4 to your computer and use it in GitHub Desktop.
Save alonsosilvaallende/08d039e00d29b0bd97ab220ad78593b4 to your computer and use it in GitHub Desktop.
Howto.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-16T20:49:58.305942Z",
"end_time": "2023-07-16T20:49:58.326576Z"
},
"trusted": true
},
"id": "5cba39c2",
"cell_type": "code",
"source": "%load_ext autoreload\n%autoreload 2",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-16T20:49:59.088824Z",
"end_time": "2023-07-16T20:49:59.115069Z"
},
"trusted": true
},
"id": "8f128ebc",
"cell_type": "code",
"source": "# get environment variable: OPENAI_API_KEY\nfrom dotenv import load_dotenv, find_dotenv\nload_dotenv(find_dotenv())",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-16T20:49:59.956300Z",
"end_time": "2023-07-16T20:50:00.067465Z"
},
"trusted": true
},
"id": "c9b1c45e",
"cell_type": "code",
"source": "import os\nimport openai\n\nopenai.api_base = \"https://openrouter.ai/api/v1\"\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-16T20:50:01.742434Z",
"end_time": "2023-07-16T20:50:01.758611Z"
},
"trusted": true
},
"id": "fd0943db",
"cell_type": "code",
"source": "OPENROUTER_REFERRER = \"https://github.com/alonsosilvaallende/langchain-streamlit\"\n\ndef llm(text: str) -> str:\n return openai.ChatCompletion.create(\n model='openai/gpt-3.5-turbo',\n headers={\n \"HTTP-Referer\": OPENROUTER_REFERRER\n },\n messages=[{\n 'role': 'user',\n 'content': f'{text}'\n }],\n temperature=0).choices[0].message.content",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2023-07-16T20:50:02.753744Z",
"end_time": "2023-07-16T20:50:05.014312Z"
},
"trusted": true
},
"id": "36669880",
"cell_type": "code",
"source": "llm(\"Tell me a joke\")",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "\"Sure, here's a classic one for you:\\n\\nWhy don't scientists trust atoms?\\n\\nBecause they make up everything!\""
},
"metadata": {}
}
]
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/alonsosilvaallende/aa0036e3d85fef791261734cb0088182"
},
"gist": {
"id": "aa0036e3d85fef791261734cb0088182",
"data": {
"description": "Howto.ipynb",
"public": true
}
},
"kernelspec": {
"name": "conda-env-langchain-py",
"display_name": "Python [conda env:langchain]",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.11.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment