Skip to content

Commit b203238

Browse files
committed
updating API examples to use Llama3 and latest langchain function signature
1 parent 2e27aae commit b203238

File tree

3 files changed

+129
-214
lines changed

3 files changed

+129
-214
lines changed

recipes/llama_api_providers/OctoAI_API_examples/Getting_to_know_Llama.ipynb

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@
1010
"Our goal in this session is to provide a guided tour of Llama 3, including understanding different Llama 3 models, how and where to access them, Generative AI and Chatbot architectures, prompt engineering, RAG (Retrieval Augmented Generation), Fine-tuning and more. All this is implemented with a starter code for you to take it and use it in your Llama 3 projects."
1111
]
1212
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {
16+
"id": "h3YGMDJidHtH"
17+
},
18+
"source": [
19+
"### **Install dependencies**"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {
26+
"id": "VhN6hXwx7FCp"
27+
},
28+
"outputs": [],
29+
"source": [
30+
"# Install dependencies and initialize\n",
31+
"%pip install \\\n",
32+
" langchain==0.1.19 \\\n",
33+
" matplotlib \\\n",
34+
" octoai-sdk==0.10.1 \\\n",
35+
" openai \\\n",
36+
" sentence_transformers \\\n",
37+
" pdf2image \\\n",
38+
" pdfminer \\\n",
39+
" pdfminer.six \\\n",
40+
" unstructured \\\n",
41+
" faiss-cpu \\\n",
42+
" pillow-heif \\\n",
43+
" opencv-python \\\n",
44+
" unstructured-inference \\\n",
45+
" pikepdf"
46+
]
47+
},
1348
{
1449
"cell_type": "markdown",
1550
"metadata": {
@@ -244,40 +279,6 @@
244279
"In this notebook, we are going to access [Llama 3 8b instruct model](https://octoai.cloud/text/chat?model=meta-llama-3-8b-instruct&mode=api) using hosted API from OctoAI."
245280
]
246281
},
247-
{
248-
"cell_type": "markdown",
249-
"metadata": {
250-
"id": "h3YGMDJidHtH"
251-
},
252-
"source": [
253-
"### **2.1 - Install dependencies**"
254-
]
255-
},
256-
{
257-
"cell_type": "code",
258-
"execution_count": null,
259-
"metadata": {
260-
"id": "VhN6hXwx7FCp"
261-
},
262-
"outputs": [],
263-
"source": [
264-
"# Install dependencies and initialize\n",
265-
"%pip install -qU \\\n",
266-
" langchain==0.1.19 \\\n",
267-
" octoai-sdk==0.10.1 \\\n",
268-
" openai \\\n",
269-
" sentence_transformers \\\n",
270-
" pdf2image \\\n",
271-
" pdfminer \\\n",
272-
" pdfminer.six \\\n",
273-
" unstructured \\\n",
274-
" faiss-cpu \\\n",
275-
" pillow-heif \\\n",
276-
" opencv-python \\\n",
277-
" unstructured-inference \\\n",
278-
" pikepdf"
279-
]
280-
},
281282
{
282283
"cell_type": "code",
283284
"execution_count": null,
@@ -359,7 +360,7 @@
359360
"id": "5Jxq0pmf6L73"
360361
},
361362
"source": [
362-
"### **2.2 - Basic completion**"
363+
"# **2.1 - Basic completion**"
363364
]
364365
},
365366
{
@@ -380,7 +381,7 @@
380381
"id": "StccjUDh6W0Q"
381382
},
382383
"source": [
383-
"### **2.3 - System prompts**\n"
384+
"## **2.2 - System prompts**\n"
384385
]
385386
},
386387
{
@@ -404,7 +405,7 @@
404405
"id": "Hp4GNa066pYy"
405406
},
406407
"source": [
407-
"### **2.4 - Response formats**\n",
408+
"### **2.3 - Response formats**\n",
408409
"* Can support different formatted outputs e.g. text, JSON, etc."
409410
]
410411
},

recipes/llama_api_providers/OctoAI_API_examples/HelloLlamaCloud.ipynb

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
"metadata": {},
77
"source": [
88
"## This demo app shows:\n",
9-
"* How to run Llama2 in the cloud hosted on OctoAI\n",
9+
"* How to run Llama 3 in the cloud hosted on OctoAI\n",
1010
"* How to use LangChain to ask Llama general questions and follow up questions\n",
11-
"* How to use LangChain to load a recent PDF doc - the Llama2 paper pdf - and chat about it. This is the well known RAG (Retrieval Augmented Generation) method to let LLM such as Llama2 be able to answer questions about the data not publicly available when Llama2 was trained, or about your own data. RAG is one way to prevent LLM's hallucination\n",
12-
"* You should also review the [HelloLlamaLocal](HelloLlamaLocal.ipynb) notebook for more information on RAG\n",
11+
"* How to use LangChain to load a recent PDF doc - the Llama paper pdf - and chat about it. This is the well known RAG (Retrieval Augmented Generation) method to let LLM such as Llama be able to answer questions about your own data. RAG is one way to prevent LLM's hallucination\n",
1312
"\n",
1413
"**Note** We will be using OctoAI to run the examples here. You will need to first sign into [OctoAI](https://octoai.cloud/) with your Github or Google account, then create a free API token [here](https://octo.ai/docs/getting-started/how-to-create-an-octoai-access-token) that you can use for a while (a month or $10 in OctoAI credits, whichever one runs out first).\n",
15-
"After the free trial ends, you will need to enter billing info to continue to use Llama2 hosted on OctoAI."
14+
"After the free trial ends, you will need to enter billing info to continue to use Llama 3 hosted on OctoAI."
1615
]
1716
},
1817
{
@@ -35,7 +34,7 @@
3534
"metadata": {},
3635
"outputs": [],
3736
"source": [
38-
"!pip install langchain octoai-sdk sentence-transformers chromadb pypdf"
37+
"%pip install langchain==0.1.19 octoai-sdk==0.10.1 openai sentence-transformers chromadb pypdf"
3938
]
4039
},
4140
{
@@ -57,15 +56,17 @@
5756
"id": "3e8870c1",
5857
"metadata": {},
5958
"source": [
60-
"Next we call the Llama 2 model from OctoAI. In this example we will use the Llama 2 13b chat FP16 model. You can find more on Llama 2 models on the [OctoAI text generation solution page](https://octoai.cloud/tools/text).\n",
59+
"Next we call the Llama 3 model from OctoAI. In this example we will use the Llama 3 8b instruct model. You can find more on Llama models on the [OctoAI text generation solution page](https://octoai.cloud/text).\n",
6160
"\n",
6261
"At the time of writing this notebook the following Llama models are available on OctoAI:\n",
63-
"* llama-2-13b-chat\n",
64-
"* llama-2-70b-chat\n",
62+
"* meta-llama-3-8b-instruct\n",
63+
"* meta-llama-3-70b-instruct\n",
6564
"* codellama-7b-instruct\n",
6665
"* codellama-13b-instruct\n",
6766
"* codellama-34b-instruct\n",
68-
"* codellama-70b-instruct"
67+
"* llama-2-13b-chat\n",
68+
"* llama-2-70b-chat\n",
69+
"* llamaguard-7b"
6970
]
7071
},
7172
{
@@ -77,21 +78,11 @@
7778
"source": [
7879
"from langchain.llms.octoai_endpoint import OctoAIEndpoint\n",
7980
"\n",
80-
"llama2_13b = \"llama-2-13b-chat-fp16\"\n",
81+
"llama3_8b = \"meta-llama-3-8b-instruct\"\n",
8182
"llm = OctoAIEndpoint(\n",
82-
" endpoint_url=\"https://text.octoai.run/v1/chat/completions\",\n",
83-
" model_kwargs={\n",
84-
" \"model\": llama2_13b,\n",
85-
" \"messages\": [\n",
86-
" {\n",
87-
" \"role\": \"system\",\n",
88-
" \"content\": \"You are a helpful, respectful and honest assistant.\"\n",
89-
" }\n",
90-
" ],\n",
91-
" \"max_tokens\": 500,\n",
92-
" \"top_p\": 1,\n",
93-
" \"temperature\": 0.01\n",
94-
" },\n",
83+
" model=llama3_8b,\n",
84+
" max_tokens=500,\n",
85+
" temperature=0.01\n",
9586
")"
9687
]
9788
},
@@ -111,7 +102,7 @@
111102
"outputs": [],
112103
"source": [
113104
"question = \"who wrote the book Innovator's dilemma?\"\n",
114-
"answer = llm(question)\n",
105+
"answer = llm.invoke(question)\n",
115106
"print(answer)"
116107
]
117108
},
@@ -134,7 +125,7 @@
134125
"source": [
135126
"# chat history not passed so Llama doesn't have the context and doesn't know this is more about the book\n",
136127
"followup = \"tell me more\"\n",
137-
"followup_answer = llm(followup)\n",
128+
"followup_answer = llm.invoke(followup)\n",
138129
"print(followup_answer)"
139130
]
140131
},
@@ -162,7 +153,7 @@
162153
"memory = ConversationBufferMemory()\n",
163154
"conversation = ConversationChain(\n",
164155
" llm=llm, \n",
165-
" memory = memory,\n",
156+
" memory=memory,\n",
166157
" verbose=False\n",
167158
")"
168159
]
@@ -208,11 +199,10 @@
208199
"id": "fc436163",
209200
"metadata": {},
210201
"source": [
211-
"Next, let's explore using Llama 2 to answer questions using documents for context. \n",
212-
"This gives us the ability to update Llama 2's knowledge thus giving it better context without needing to finetune. \n",
213-
"For a more in-depth study of this, see the notebook on using Llama 2 locally [here](HelloLlamaLocal.ipynb)\n",
202+
"Next, let's explore using Llama 3 to answer questions using documents for context. \n",
203+
"This gives us the ability to update Llama 3's knowledge thus giving it better context without needing to finetune. \n",
214204
"\n",
215-
"We will use the PyPDFLoader to load in a pdf, in this case, the Llama 2 paper."
205+
"We will use the PyPDFLoader to load in a pdf, in this case, the Llama paper."
216206
]
217207
},
218208
{
@@ -301,7 +291,7 @@
301291
"id": "54ad02d7",
302292
"metadata": {},
303293
"source": [
304-
"We then use ` RetrievalQA` to retrieve the documents from the vector database and give the model more context on Llama 2, thereby increasing its knowledge.\n",
294+
"We then use ` RetrievalQA` to retrieve the documents from the vector database and give the model more context on Llama, thereby increasing its knowledge.\n",
305295
"\n",
306296
"For each question, LangChain performs a semantic similarity search of it in the vector db, then passes the search results as the context to Llama to answer the question."
307297
]
@@ -321,7 +311,7 @@
321311
" retriever=vectordb.as_retriever()\n",
322312
")\n",
323313
"\n",
324-
"question = \"What is llama2?\"\n",
314+
"question = \"What is llama?\"\n",
325315
"result = qa_chain({\"query\": question})\n",
326316
"print(result['result'])"
327317
]
@@ -344,7 +334,7 @@
344334
"metadata": {},
345335
"outputs": [],
346336
"source": [
347-
"# no context passed so Llama2 doesn't have enough context to answer so it lets its imagination go wild\n",
337+
"# no context passed so Llama doesn't have enough context to answer so it lets its imagination go wild\n",
348338
"result = qa_chain({\"query\": \"what are its use cases?\"})\n",
349339
"print(result['result'])"
350340
]
@@ -376,7 +366,7 @@
376366
"metadata": {},
377367
"outputs": [],
378368
"source": [
379-
"# let's ask the original question \"What is llama2?\" again\n",
369+
"# let's ask the original question \"What is llama?\" again\n",
380370
"result = chat_chain({\"question\": question, \"chat_history\": []})\n",
381371
"print(result['answer'])"
382372
]

0 commit comments

Comments
 (0)