Chat loaders | 🦜🔗 Langchain

Main:  [Skip to main content](#docusaurus_skipToContent_fallback)  [ 🦜🔗 LangChain ](/)  Search CTRLK - [](/)- [Components](/docs/integrations/components)- Chat loaders #Chat loaders [ ##📄 Discord This notebook shows how to create your own chat loader that works on copy-pasted messages (from dms) to a list of LangChain messages.  ](/docs/integrations/chat_loaders/discord) [ ##📄 Facebook Messenger This notebook shows how to load data from Facebook in a format you can fine-tune on. The overall steps are:  ](/docs/integrations/chat_loaders/facebook) [ ##📄 GMail This loader goes over how to load data from GMail. There are many ways you could want to load data from GMail. This loader is currently fairly opinionated in how to do so. The way it does it is it first looks for all messages that you have sent. It then looks for messages where you are responding to a previous email. It then fetches that previous email, and creates a training example of that email, followed by your email.  ](/docs/integrations/chat_loaders/gmail) [ ##📄 iMessage This notebook shows how to use the iMessage chat loader. This class helps convert iMessage conversations to LangChain chat messages.  ](/docs/integrations/chat_loaders/imessage) [ ##📄 LangSmith Chat Datasets This notebook demonstrates an easy way to load a LangSmith chat dataset fine-tune a model on that data.  ](/docs/integrations/chat_loaders/langsmith_dataset) [ ##📄 LangSmith LLM Runs This notebook demonstrates how to directly load data from LangSmith's LLM runs and fine-tune a model on that data.  ](/docs/integrations/chat_loaders/langsmith_llm_runs) [ ##📄 Slack This notebook shows how to use the Slack chat loader. This class helps map exported slack conversations to LangChain chat messages.  ](/docs/integrations/chat_loaders/slack) [ ##📄 Telegram This notebook shows how to use the Telegram chat loader. This class helps map exported Telegram conversations to LangChain chat messages.  ](/docs/integrations/chat_loaders/telegram) [ ##📄 Twitter (via Apify) This notebook shows how to load chat messages from Twitter to fine-tune on. We do this by utilizing Apify.  ](/docs/integrations/chat_loaders/twitter) [ ##📄 WeChat There is not yet a straightforward way to export personal WeChat messages. However if you just need no more than few hundreds of messages for model fine-tuning or few-shot examples, this notebook shows how to create your own chat loader that works on copy-pasted WeChat messages to a list of LangChain messages.  ](/docs/integrations/chat_loaders/wechat) [ ##📄 WhatsApp This notebook shows how to use the WhatsApp chat loader. This class helps map exported WhatsApp conversations to LangChain chat messages.  ](/docs/integrations/chat_loaders/whatsapp) [ Previous Trubrics ](/docs/integrations/callbacks/trubrics) [ Next Discord ](/docs/integrations/chat_loaders/discord) Community - [Discord](https://discord.gg/cU2adEyC7w) - [Twitter](https://twitter.com/LangChainAI) GitHub - [Python](https://github.com/langchain-ai/langchain) - [JS/TS](https://github.com/langchain-ai/langchainjs) More - [Homepage](https://langchain.com) - [Blog](https://blog.langchain.dev) Copyright © 2023 LangChain, Inc.


Discord | 🦜🔗 Langchain

Main:  On this page #Discord This notebook shows how to create your own chat loader that works on copy-pasted messages (from dms) to a list of LangChain messages.  The process has four steps:  - Create the chat .txt file by copying chats from the Discord app and pasting them in a file on your local computer - Copy the chat loader definition from below to a local file. - Initialize the DiscordChatLoader with the file path pointed to the text file. - Call loader.load() (or loader.lazy_load()) to perform the conversion. ##1. Create message dump[](#1-create-message-dump) Currently (2023/08/23) this loader only supports .txt files in the format generated by copying messages in the app to your clipboard and pasting in a file. Below is an example.  discord_chats.txt talkingtower — 08/15/2023 11:10 AM Love music! Do you like jazz? reporterbob — 08/15/2023 9:27 PM Yes! Jazz is fantastic. Ever heard this one? Website Listen to classic jazz track...  talkingtower — Yesterday at 5:03 AM Indeed! Great choice. 🎷 reporterbob — Yesterday at 5:23 AM Thanks! How about some virtual sightseeing? Website Virtual tour of famous landmarks...  talkingtower — Today at 2:38 PM Sounds fun! Let's explore. reporterbob — Today at 2:56 PM Enjoy the tour! See you around. talkingtower — Today at 3:00 PM Thank you! Goodbye! 👋 reporterbob — Today at 3:02 PM Farewell! Happy exploring.  ##2. Define chat loader[](#2-define-chat-loader) import logging import re from typing import Iterator, List  from langchain.chat_loaders import base as chat_loaders from langchain.schema import BaseMessage, HumanMessage  logger = logging.getLogger()   class DiscordChatLoader(chat_loaders.BaseChatLoader):     def __init__(self, path: str):         """         Initialize the Discord chat loader.          Args:             path: Path to the exported Discord chat text file.         """         self.path = path         self._message_line_regex = re.compile(             r"(.+?) — (\w{3,9} \d{1,2}(?:st|nd|rd|th)?(?:, \d{4})? \d{1,2}:\d{2} (?:AM|PM)|Today at \d{1,2}:\d{2} (?:AM|PM)|Yesterday at \d{1,2}:\d{2} (?:AM|PM))",  # noqa             flags=re.DOTALL,         )      def _load_single_chat_session_from_txt(         self, file_path: str     ) -> chat_loaders.ChatSession:         """         Load a single chat session from a text file.          Args:             file_path: Path to the text file containing the chat messages.          Returns:             A `ChatSession` object containing the loaded chat messages.         """         with open(file_path, "r", encoding="utf-8") as file:             lines = file.readlines()          results: List[BaseMessage] = []         current_sender = None         current_timestamp = None         current_content = []         for line in lines:             if re.match(                 r".+? — (\d{2}/\d{2}/\d{4} \d{1,2}:\d{2} (?:AM|PM)|Today at \d{1,2}:\d{2} (?:AM|PM)|Yesterday at \d{1,2}:\d{2} (?:AM|PM))",  # noqa                 line,             ):                 if current_sender and current_content:                     results.append(                         HumanMessage(                             content="".join(current_content).strip(),                             additional_kwargs={                                 "sender": current_sender,                                 "events": [{"message_time": current_timestamp}],                             },                         )                     )                 current_sender, current_timestamp = line.split(" — ")[:2]                 current_content = [                     line[len(current_sender) + len(current_timestamp) + 4 :].strip()                 ]             elif re.match(r"\[\d{1,2}:\d{2} (?:AM|PM)\]", line.strip()):                 results.append(                     HumanMessage(                         content="".join(current_content).strip(),                         additional_kwargs={                             "sender": current_sender,                             "events": [{"message_time": current_timestamp}],                         },                     )                 )                 current_timestamp = line.strip()[1:-1]                 current_content = []             else:                 current_content.append("\n" + line.strip())          if current_sender and current_content:             results.append(                 HumanMessage(                     content="".join(current_content).strip(),                     additional_kwargs={                         "sender": current_sender,                         "events": [{"message_time": current_timestamp}],                     },                 )             )          return chat_loaders.ChatSession(messages=results)      def lazy_load(self) -> Iterator[chat_loaders.ChatSession]:         """         Lazy load the messages from the chat file and yield them in the required format.          Yields:             A `ChatSession` object containing the loaded chat messages.         """         yield self._load_single_chat_session_from_txt(self.path)  ##2. Create loader[](#2-create-loader) We will point to the file we just wrote to disk.  loader = DiscordChatLoader(     path="./discord_chats.txt", )  ##3. Load Messages[](#3-load-messages) Assuming the format is correct, the loader will convert the chats to langchain messages.  from typing import List  from langchain.chat_loaders.base import ChatSession from langchain.chat_loaders.utils import (     map_ai_messages,     merge_chat_runs, )  raw_messages = loader.lazy_load() # Merge consecutive messages from the same sender into a single message merged_messages = merge_chat_runs(raw_messages) # Convert messages from "talkingtower" to AI messages messages: List[ChatSession] = list(     map_ai_messages(merged_messages, sender="talkingtower") )  messages      [{'messages': [AIMessage(content='Love music! Do you like jazz?', additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': '08/15/2023 11:10 AM\n'}]}),        Human


Discord | 🦜🔗 Langchain

Message(content='Yes! Jazz is fantastic. Ever heard this one?\nWebsite\nListen to classic jazz track...', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': '08/15/2023 9:27 PM\n'}]}),        AIMessage(content='Indeed! Great choice. 🎷', additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': 'Yesterday at 5:03 AM\n'}]}),        HumanMessage(content='Thanks! How about some virtual sightseeing?\nWebsite\nVirtual tour of famous landmarks...', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': 'Yesterday at 5:23 AM\n'}]}),        AIMessage(content="Sounds fun! Let's explore.", additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': 'Today at 2:38 PM\n'}]}),        HumanMessage(content='Enjoy the tour! See you around.', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': 'Today at 2:56 PM\n'}]}),        AIMessage(content='Thank you! Goodbye! 👋', additional_kwargs={'sender': 'talkingtower', 'events': [{'message_time': 'Today at 3:00 PM\n'}]}),        HumanMessage(content='Farewell! Happy exploring.', additional_kwargs={'sender': 'reporterbob', 'events': [{'message_time': 'Today at 3:02 PM\n'}]})]}]  ###Next Steps[](#next-steps) You can then use these messages how you see fit, such as fine-tuning a model, few-shot example selection, or directly make predictions for the next message  from langchain.chat_models import ChatOpenAI  llm = ChatOpenAI()  for chunk in llm.stream(messages[0]["messages"]):     print(chunk.content, end="", flush=True)      Thank you! Have a great day!


Facebook Messenger | 🦜🔗 Langchain

Main:  Skip to main content  🦜🔗 LangChain  Search CTRLK ComponentsChat loadersFacebook Messenger On this page Facebook Messenger This notebook shows how to load data from Facebook in a format you can fine-tune on. The overall steps are:  Download your messenger data to disk. Create the Chat Loader and call loader.load() (or loader.lazy_load()) to perform the conversion. Optionally use merge_chat_runs to combine message from the same sender in sequence, and/or map_ai_messages to convert messages from the specified sender to the "AIMessage" class. Once you've done this, call convert_messages_for_finetuning to prepare your data for fine-tuning. Once this has been done, you can fine-tune your model. To do so you would complete the following steps:  Upload your messages to OpenAI and run a fine-tuning job. Use the resulting model in your LangChain app! Let's begin.  1. Download Data To download your own messenger data, following instructions here. IMPORTANT - make sure to download them in JSON format (not HTML).  We are hosting an example dump at this google drive link that we will use in this walkthrough.  # This uses some example data import zipfile  import requests   def download_and_unzip(url: str, output_path: str = "file.zip") -> None:     file_id = url.split("/")[-2]     download_url = f"https://drive.google.com/uc?export=download&id={file_id}"      response = requests.get(download_url)     if response.status_code != 200:         print("Failed to download the file.")         return      with open(output_path, "wb") as file:         file.write(response.content)         print(f"File {output_path} downloaded.")      with zipfile.ZipFile(output_path, "r") as zip_ref:         zip_ref.extractall()         print(f"File {output_path} has been unzipped.")   # URL of the file to download url = (     "https://drive.google.com/file/d/1rh1s1o2i7B-Sk1v9o8KNgivLVGwJ-osV/view?usp=sharing" )  # Download and unzip download_and_unzip(url)      File file.zip downloaded.     File file.zip has been unzipped.  2. Create Chat Loader We have 2 different FacebookMessengerChatLoader classes, one for an entire directory of chats, and one to load individual files. We  directory_path = "./hogwarts"  from langchain.chat_loaders.facebook_messenger import (     FolderFacebookMessengerChatLoader,     SingleFileFacebookMessengerChatLoader, )  loader = SingleFileFacebookMessengerChatLoader(     path="./hogwarts/inbox/HermioneGranger/messages_Hermione_Granger.json", )  chat_session = loader.load()[0] chat_session["messages"][:3]      [HumanMessage(content="Hi Hermione! How's your summer going so far?", additional_kwargs={'sender': 'Harry Potter'}),      HumanMessage(content="Harry! Lovely to hear from you. My summer is going well, though I do miss everyone. I'm spending most of my time going through my books and researching fascinating new topics. How about you?", additional_kwargs={'sender': 'Hermione Granger'}),      HumanMessage(content="I miss you all too. The Dursleys are being their usual unpleasant selves but I'm getting by. At least I can practice some spells in my room without them knowing. Let me know if you find anything good in your researching!", additional_kwargs={'sender': 'Harry Potter'})]  loader = FolderFacebookMessengerChatLoader(     path="./hogwarts", )  chat_sessions = loader.load() len(chat_sessions)      9  3. Prepare for fine-tuning Calling load() returns all the chat messages we could extract as human messages. When conversing with chat bots, conversations typically follow a more strict alternating dialogue pattern relative to real conversations.  You can choose to merge message "runs" (consecutive messages from the same sender) and select a sender to represent the "AI". The fine-tuned LLM will learn to generate these AI messages.  from langchain.chat_loaders.utils import (     map_ai_messages,     merge_chat_runs, )  merged_sessions = merge_chat_runs(chat_sessions) alternating_sessions = list(map_ai_messages(merged_sessions, "Harry Potter"))  # Now all of Harry Potter's messages will take the AI message class # which maps to the 'assistant' role in OpenAI's training format alternating_sessions[0]["messages"][:3]      [AIMessage(content="Professor Snape, I was hoping I could speak with you for a moment about something that's been concerning me lately.", additional_kwargs={'sender': 'Harry Potter'}),      HumanMessage(content="What is it, Potter? I'm quite busy at the moment.", additional_kwargs={'sender': 'Severus Snape'}),      AIMessage(content="I apologize for the interruption, sir. I'll be brief. I've noticed some strange activity around the school grounds at night. I saw a cloaked figure lurking near the Forbidden Forest last night. I'm worried someone may be plotting something sinister.", additional_kwargs={'sender': 'Harry Potter'})]  Now we can convert to OpenAI format dictionaries from langchain.adapters.openai import convert_messages_for_finetuning  training_data = convert_messages_for_finetuning(alternating_sessions) print(f"Prepared {len(training_data)} dialogues for training")      Prepared 9 dialogues for training  training_data[0][:3]      [{'role': 'assistant',       'content': "Professor Snape, I was hoping I could speak with you for a moment about something that's been concerning me lately."},      {'role': 'user',       'content': "What is it, Potter? I'm quite busy at the moment."},      {'role': 'assistant',       'content': "I apologize for the interruption, sir. I'll be brief. I've noticed some strange activity around the school grounds at night. I saw a cloaked figure lurking near the Forbidden Forest last night. I'm worried someone may be plotting something sinister."}]  OpenAI currently requires at least 10 training examples for a fine-tuning job, though they recommend between 50-100 for most tasks. Since we only have 9 chat sessions, we can subdivide them (optionally with some overlap) so that each training example is comprised of a portion of a whole conversation.


Facebook Messenger | 🦜🔗 Langchain

Facebook chat sessions (1 per person) often span multiple days and conversations, so the long-range dependencies may not be that important to model anyhow.  # Our chat is alternating, we will make each datapoint a group of 8 messages, # with 2 messages overlapping chunk_size = 8 overlap = 2  training_examples = [     conversation_messages[i : i + chunk_size]     for conversation_messages in training_data     for i in range(0, len(conversation_messages) - chunk_size + 1, chunk_size - overlap) ]  len(training_examples)      100  4. Fine-tune the model It's time to fine-tune the model. Make sure you have openai installed and have set your OPENAI_API_KEY appropriately  # %pip install -U openai --quiet  import json import time from io import BytesIO  import openai  # We will write the jsonl file in memory my_file = BytesIO() for m in training_examples:     my_file.write((json.dumps({"messages": m}) + "\n").encode("utf-8"))  my_file.seek(0) training_file = openai.files.create(file=my_file, purpose="fine-tune")  # OpenAI audits each training file for compliance reasons. # This make take a few minutes status = openai.files.retrieve(training_file.id).status start_time = time.time() while status != "processed":     print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True)     time.sleep(5)     status = openai.files.retrieve(training_file.id).status print(f"File {training_file.id} ready after {time.time() - start_time:.2f} seconds.")      File file-ULumAXLEFw3vB6bb9uy6DNVC ready after 0.00 seconds.  With the file ready, it's time to kick off a training job.  job = openai.fine_tuning.jobs.create(     training_file=training_file.id,     model="gpt-3.5-turbo", )  Grab a cup of tea while your model is being prepared. This may take some time!  status = openai.fine_tuning.jobs.retrieve(job.id).status start_time = time.time() while status != "succeeded":     print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True)     time.sleep(5)     job = openai.fine_tuning.jobs.retrieve(job.id)     status = job.status      Status=[running]... 874.29s. 56.93s  print(job.fine_tuned_model)      ft:gpt-3.5-turbo-0613:personal::8QnAzWMr  5. Use in LangChain You can use the resulting model ID directly the ChatOpenAI model class.  from langchain.chat_models import ChatOpenAI  model = ChatOpenAI(     model=job.fine_tuned_model,     temperature=1, )  from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser  prompt = ChatPromptTemplate.from_messages(     [         ("human", "{input}"),     ] )  chain = prompt | model | StrOutputParser()  for tok in chain.stream({"input": "What classes are you taking?"}):     print(tok, end="", flush=True)      I'm taking Charms, Defense Against the Dark Arts, Herbology, Potions, Transfiguration, and Ancient Runes. How about you?  Previous Discord Next GMail Community Discord Twitter GitHub Python JS/TS More Homepage Blog Copyright © 2023 LangChain, Inc.


GMail | 🦜🔗 Langchain

Main:  #GMail This loader goes over how to load data from GMail. There are many ways you could want to load data from GMail. This loader is currently fairly opinionated in how to do so. The way it does it is it first looks for all messages that you have sent. It then looks for messages where you are responding to a previous email. It then fetches that previous email, and creates a training example of that email, followed by your email.  Note that there are clear limitations here. For example, all examples created are only looking at the previous email for context.  To use:  - Set up a Google Developer Account: Go to the Google Developer Console, create a project, and enable the Gmail API for that project. This will give you a credentials.json file that you'll need later.  - Install the Google Client Library: Run the following command to install the Google Client Library:  pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client  import os.path  from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow  SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]   creds = None # The file token.json stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists("email_token.json"):     creds = Credentials.from_authorized_user_file("email_token.json", SCOPES) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid:     if creds and creds.expired and creds.refresh_token:         creds.refresh(Request())     else:         flow = InstalledAppFlow.from_client_secrets_file(             # your creds file here. Please create json file as here https://cloud.google.com/docs/authentication/getting-started             "creds.json",             SCOPES,         )         creds = flow.run_local_server(port=0)     # Save the credentials for the next run     with open("email_token.json", "w") as token:         token.write(creds.to_json())  from langchain.chat_loaders.gmail import GMailLoader  loader = GMailLoader(creds=creds, n=3)  data = loader.load()  # Sometimes there can be errors which we silently ignore len(data)      2  from langchain.chat_loaders.utils import (     map_ai_messages, )  # This makes messages sent by hchase@langchain.com the AI Messages # This means you will train an LLM to predict as if it's responding as hchase training_data = list(     map_ai_messages(data, sender="Harrison Chase <hchase@langchain.com>") )


iMessage | 🦜🔗 Langchain

Main:  Skip to main content  🦜🔗 LangChain  Search CTRLK ComponentsChat loadersiMessage On this page iMessage This notebook shows how to use the iMessage chat loader. This class helps convert iMessage conversations to LangChain chat messages.  On MacOS, iMessage stores conversations in a sqlite database at ~/Library/Messages/chat.db (at least for macOS Ventura 13.4). The IMessageChatLoader loads from this database file.  Create the IMessageChatLoader with the file path pointed to chat.db database you'd like to process. Call loader.load() (or loader.lazy_load()) to perform the conversion. Optionally use merge_chat_runs to combine message from the same sender in sequence, and/or map_ai_messages to convert messages from the specified sender to the "AIMessage" class. 1. Access Chat DB It's likely that your terminal is denied access to ~/Library/Messages. To use this class, you can copy the DB to an accessible directory (e.g., Documents) and load from there. Alternatively (and not recommended), you can grant full disk access for your terminal emulator in System Settings > Security and Privacy > Full Disk Access.  We have created an example database you can use at this linked drive file.  # This uses some example data import requests   def download_drive_file(url: str, output_path: str = "chat.db") -> None:     file_id = url.split("/")[-2]     download_url = f"https://drive.google.com/uc?export=download&id={file_id}"      response = requests.get(download_url)     if response.status_code != 200:         print("Failed to download the file.")         return      with open(output_path, "wb") as file:         file.write(response.content)         print(f"File {output_path} downloaded.")   url = (     "https://drive.google.com/file/d/1NebNKqTA2NXApCmeH6mu0unJD2tANZzo/view?usp=sharing" )  # Download file to chat.db download_drive_file(url)      File chat.db downloaded.  2. Create the Chat Loader Provide the loader with the file path to the zip directory. You can optionally specify the user id that maps to an ai message as well an configure whether to merge message runs.  from langchain.chat_loaders.imessage import IMessageChatLoader  loader = IMessageChatLoader(     path="./chat.db", )  3. Load messages The load() (or lazy_load) methods return a list of "ChatSessions" that currently just contain a list of messages per loaded conversation. All messages are mapped to "HumanMessage" objects to start.  You can optionally choose to merge message "runs" (consecutive messages from the same sender) and select a sender to represent the "AI". The fine-tuned LLM will learn to generate these AI messages.  from typing import List  from langchain.chat_loaders.base import ChatSession from langchain.chat_loaders.utils import (     map_ai_messages,     merge_chat_runs, )  raw_messages = loader.lazy_load() # Merge consecutive messages from the same sender into a single message merged_messages = merge_chat_runs(raw_messages) # Convert messages from "Tortoise" to AI messages. Do you have a guess who these conversations are between? chat_sessions: List[ChatSession] = list(     map_ai_messages(merged_messages, sender="Tortoise") )  # Now all of the Tortoise's messages will take the AI message class # which maps to the 'assistant' role in OpenAI's training format alternating_sessions[0]["messages"][:3]      [AIMessage(content="Slow and steady, that's my motto.", additional_kwargs={'message_time': 1693182723, 'sender': 'Tortoise'}, example=False),      HumanMessage(content='Speed is key!', additional_kwargs={'message_time': 1693182753, 'sender': 'Hare'}, example=False),      AIMessage(content='A balanced approach is more reliable.', additional_kwargs={'message_time': 1693182783, 'sender': 'Tortoise'}, example=False)]  3. Prepare for fine-tuning Now it's time to convert our chat messages to OpenAI dictionaries. We can use the convert_messages_for_finetuning utility to do so.  from langchain.adapters.openai import convert_messages_for_finetuning  training_data = convert_messages_for_finetuning(alternating_sessions) print(f"Prepared {len(training_data)} dialogues for training")      Prepared 10 dialogues for training  4. Fine-tune the model It's time to fine-tune the model. Make sure you have openai installed and have set your OPENAI_API_KEY appropriately  # %pip install -U openai --quiet  import json import time from io import BytesIO  import openai  # We will write the jsonl file in memory my_file = BytesIO() for m in training_data:     my_file.write((json.dumps({"messages": m}) + "\n").encode("utf-8"))  my_file.seek(0) training_file = openai.File.create(file=my_file, purpose="fine-tune")  # OpenAI audits each training file for compliance reasons. # This make take a few minutes status = openai.File.retrieve(training_file.id).status start_time = time.time() while status != "processed":     print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True)     time.sleep(5)     status = openai.File.retrieve(training_file.id).status print(f"File {training_file.id} ready after {time.time() - start_time:.2f} seconds.")      File file-zHIgf4r8LltZG3RFpkGd4Sjf ready after 10.19 seconds.  With the file ready, it's time to kick off a training job.  job = openai.FineTuningJob.create(     training_file=training_file.id,     model="gpt-3.5-turbo", )  Grab a cup of tea while your model is being prepared. This may take some time!  status = openai.FineTuningJob.retrieve(job.id).status start_time = time.time() while status != "succeeded":     print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True)     time.sleep(5)     job = openai.FineTuningJob.retrieve(job.id)     status = job.status      Status=[running]... 524.95s  print(job.fine_tuned_model)      ft:gpt-3.5-turbo-0613:personal::7sKoRdlz  5. Use in LangChain You can use the resulting model ID directly the ChatOpenAI model class.  from langchain.chat_models import ChatOpenAI  model = ChatOpenAI(     model=job.fine_tuned_model,     tempe


iMessage | 🦜🔗 Langchain

rature=1, )  from langchain.prompts import ChatPromptTemplate from langchain.schema.output_parser import StrOutputParser  prompt = ChatPromptTemplate.from_messages(     [         ("system", "You are speaking to hare."),         ("human", "{input}"),     ] )  chain = prompt | model | StrOutputParser()  for tok in chain.stream({"input": "What's the golden thread?"}):     print(tok, end="", flush=True)      A symbol of interconnectedness.  Previous GMail Next LangSmith Chat Datasets Community Discord Twitter GitHub Python JS/TS More Homepage Blog Copyright © 2023 LangChain, Inc.


LangSmith Chat Datasets | 🦜🔗 Langchain

Main:  On this page #LangSmith Chat Datasets This notebook demonstrates an easy way to load a LangSmith chat dataset fine-tune a model on that data. The process is simple and comprises 3 steps.  - Create the chat dataset. - Use the LangSmithDatasetChatLoader to load examples. - Fine-tune your model. Then you can use the fine-tuned model in your LangChain app.  Before diving in, let's install our prerequisites.  ##Prerequisites[](#prerequisites) Ensure you've installed langchain >= 0.0.311 and have configured your environment with your LangSmith API key.  %pip install -U langchain openai  import os import uuid  uid = uuid.uuid4().hex[:6] os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "YOUR API KEY"  ##1. Select a dataset[](#1-select-a-dataset) This notebook fine-tunes a model directly on selecting which runs to fine-tune on. You will often curate these from traced runs. You can learn more about LangSmith datasets in the docs [docs](https://docs.smith.langchain.com/evaluation/datasets).  For the sake of this tutorial, we will upload an existing dataset here that you can use.  from langsmith.client import Client  client = Client()  import requests  url = "https://raw.githubusercontent.com/langchain-ai/langchain/master/docs/docs/integrations/chat_loaders/example_data/langsmith_chat_dataset.json" response = requests.get(url) response.raise_for_status() data = response.json()  dataset_name = f"Extraction Fine-tuning Dataset {uid}" ds = client.create_dataset(dataset_name=dataset_name, data_type="chat")  _ = client.create_examples(     inputs=[e["inputs"] for e in data],     outputs=[e["outputs"] for e in data],     dataset_id=ds.id, )  ##2. Prepare Data[](#2-prepare-data) Now we can create an instance of LangSmithRunChatLoader and load the chat sessions using its lazy_load() method.  from langchain.chat_loaders.langsmith import LangSmithDatasetChatLoader  loader = LangSmithDatasetChatLoader(dataset_name=dataset_name)  chat_sessions = loader.lazy_load()  ####With the chat sessions loaded, convert them into a format suitable for fine-tuning.[](#with-the-chat-sessions-loaded-convert-them-into-a-format-suitable-for-fine-tuning) from langchain.adapters.openai import convert_messages_for_finetuning  training_data = convert_messages_for_finetuning(chat_sessions)  ##3. Fine-tune the Model[](#3-fine-tune-the-model) Now, initiate the fine-tuning process using the OpenAI library.  import json import time from io import BytesIO  import openai  my_file = BytesIO() for dialog in training_data:     my_file.write((json.dumps({"messages": dialog}) + "\n").encode("utf-8"))  my_file.seek(0) training_file = openai.File.create(file=my_file, purpose="fine-tune")  job = openai.FineTuningJob.create(     training_file=training_file.id,     model="gpt-3.5-turbo", )  # Wait for the fine-tuning to complete (this may take some time) status = openai.FineTuningJob.retrieve(job.id).status start_time = time.time() while status != "succeeded":     print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True)     time.sleep(5)     status = openai.FineTuningJob.retrieve(job.id).status  # Now your model is fine-tuned!      Status=[running]... 302.42s. 143.85s  ##4. Use in LangChain[](#4-use-in-langchain) After fine-tuning, use the resulting model ID with the ChatOpenAI model class in your LangChain app.  # Get the fine-tuned model ID job = openai.FineTuningJob.retrieve(job.id) model_id = job.fine_tuned_model  # Use the fine-tuned model in LangChain model = ChatOpenAI(     model=model_id,     temperature=1, )  model.invoke("There were three ravens sat on a tree.")  Now you have successfully fine-tuned a model using data from LangSmith LLM runs!


LangSmith LLM Runs | 🦜🔗 Langchain

Main:  Skip to main content  🦜🔗 LangChain  Search CTRLK ComponentsChat loadersLangSmith LLM Runs On this page LangSmith LLM Runs This notebook demonstrates how to directly load data from LangSmith's LLM runs and fine-tune a model on that data. The process is simple and comprises 3 steps.  Select the LLM runs to train on. Use the LangSmithRunChatLoader to load runs as chat sessions. Fine-tune your model. Then you can use the fine-tuned model in your LangChain app.  Before diving in, let's install our prerequisites.  Prerequisites Ensure you've installed langchain >= 0.0.311 and have configured your environment with your LangSmith API key.  %pip install -U langchain openai  import os import uuid  uid = uuid.uuid4().hex[:6] project_name = f"Run Fine-tuning Walkthrough {uid}" os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_API_KEY"] = "YOUR API KEY" os.environ["LANGCHAIN_PROJECT"] = project_name  1. Select Runs The first step is selecting which runs to fine-tune on. A common case would be to select LLM runs within traces that have received positive user feedback. You can find examples of this in theLangSmith Cookbook and in the docs.  For the sake of this tutorial, we will generate some runs for you to use here. Let's try fine-tuning a simple function-calling chain.  from enum import Enum  from langchain.pydantic_v1 import BaseModel, Field   class Operation(Enum):     add = "+"     subtract = "-"     multiply = "*"     divide = "/"   class Calculator(BaseModel):     """A calculator function"""      num1: float     num2: float     operation: Operation = Field(..., description="+,-,*,/")      def calculate(self):         if self.operation == Operation.add:             return self.num1 + self.num2         elif self.operation == Operation.subtract:             return self.num1 - self.num2         elif self.operation == Operation.multiply:             return self.num1 * self.num2         elif self.operation == Operation.divide:             if self.num2 != 0:                 return self.num1 / self.num2             else:                 return "Cannot divide by zero"  from pprint import pprint  from langchain.pydantic_v1 import BaseModel from langchain.utils.openai_functions import convert_pydantic_to_openai_function  openai_function_def = convert_pydantic_to_openai_function(Calculator) pprint(openai_function_def)      {'description': 'A calculator function',      'name': 'Calculator',      'parameters': {'description': 'A calculator function',                     'properties': {'num1': {'title': 'Num1', 'type': 'number'},                                    'num2': {'title': 'Num2', 'type': 'number'},                                    'operation': {'allOf': [{'description': 'An '                                                                            'enumeration.',                                                             'enum': ['+',                                                                      '-',                                                                      '*',                                                                      '/'],                                                             'title': 'Operation'}],                                                  'description': '+,-,*,/'}},                     'required': ['num1', 'num2', 'operation'],                     'title': 'Calculator',                     'type': 'object'}}  from langchain.chat_models import ChatOpenAI from langchain.output_parsers.openai_functions import PydanticOutputFunctionsParser from langchain.prompts import ChatPromptTemplate  prompt = ChatPromptTemplate.from_messages(     [         ("system", "You are an accounting assistant."),         ("user", "{input}"),     ] ) chain = (     prompt     | ChatOpenAI().bind(functions=[openai_function_def])     | PydanticOutputFunctionsParser(pydantic_schema=Calculator)     | (lambda x: x.calculate()) )  math_questions = [     "What's 45/9?",     "What's 81/9?",     "What's 72/8?",     "What's 56/7?",     "What's 36/6?",     "What's 64/8?",     "What's 12*6?",     "What's 8*8?",     "What's 10*10?",     "What's 11*11?",     "What's 13*13?",     "What's 45+30?",     "What's 72+28?",     "What's 56+44?",     "What's 63+37?",     "What's 70-35?",     "What's 60-30?",     "What's 50-25?",     "What's 40-20?",     "What's 30-15?", ] results = chain.batch([{"input": q} for q in math_questions], return_exceptions=True)      Retrying langchain.chat_models.openai.ChatOpenAI.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised ServiceUnavailableError: The server is overloaded or not ready yet..  Load runs that did not error Now we can select the successful runs to fine-tune on.  from langsmith.client import Client  client = Client()  successful_traces = {     run.trace_id     for run in client.list_runs(         project_name=project_name,         execution_order=1,         error=False,     ) }  llm_runs = [     run     for run in client.list_runs(         project_name=project_name,         run_type="llm",     )     if run.trace_id in successful_traces ]  2. Prepare data Now we can create an instance of LangSmithRunChatLoader and load the chat sessions using its lazy_load() method.  from langchain.chat_loaders.langsmith import LangSmithRunChatLoader  loader = LangSmithRunChatLoader(runs=llm_runs)  chat_sessions = loader.lazy_load()  With the chat sessions loaded, convert them into a format suitable for fine-tuning. from langchain.adapters.openai import convert_messages_for_finetuning  training_data = convert_messages_for_finetuning(chat_sessions)  3. Fine-tune the model Now, initiate the fine-tuning process using the OpenAI library.  import json import time from io import BytesIO  import openai  my_file = BytesIO() for dialog in training_data:     my_file.write((json.dumps({"messages": dialog}) + "\n").encode("utf-8"))  my_file.seek(0) training_file = openai.File.create(file=my_file, purpose="fine-tune")  job


LangSmith LLM Runs | 🦜🔗 Langchain

= openai.FineTuningJob.create(     training_file=training_file.id,     model="gpt-3.5-turbo", )  # Wait for the fine-tuning to complete (this may take some time) status = openai.FineTuningJob.retrieve(job.id).status start_time = time.time() while status != "succeeded":     print(f"Status=[{status}]... {time.time() - start_time:.2f}s", end="\r", flush=True)     time.sleep(5)     status = openai.FineTuningJob.retrieve(job.id).status  # Now your model is fine-tuned!      Status=[running]... 346.26s. 31.70s  4. Use in LangChain After fine-tuning, use the resulting model ID with the ChatOpenAI model class in your LangChain app.  # Get the fine-tuned model ID job = openai.FineTuningJob.retrieve(job.id) model_id = job.fine_tuned_model  # Use the fine-tuned model in LangChain model = ChatOpenAI(     model=model_id,     temperature=1, )  (prompt | model).invoke({"input": "What's 56/7?"})      AIMessage(content='{\n  "num1": 56,\n  "num2": 7,\n  "operation": "/"\n}')  Now you have successfully fine-tuned a model using data from LangSmith LLM runs!  Previous LangSmith Chat Datasets Next Slack Community Discord Twitter GitHub Python JS/TS More Homepage Blog Copyright © 2023 LangChain, Inc.


Slack | 🦜🔗 Langchain

Main:  On this page #Slack This notebook shows how to use the Slack chat loader. This class helps map exported slack conversations to LangChain chat messages.  The process has three steps:  - Export the desired conversation thread by following the [instructions here](https://slack.com/help/articles/1500001548241-Request-to-export-all-conversations). - Create the SlackChatLoader with the file path pointed to the json file or directory of JSON files - Call loader.load() (or loader.lazy_load()) to perform the conversion. Optionally use merge_chat_runs to combine message from the same sender in sequence, and/or map_ai_messages to convert messages from the specified sender to the "AIMessage" class. ##1. Create message dump[](#1-create-message-dump) Currently (2023/08/23) this loader best supports a zip directory of files in the format generated by exporting your a direct message conversation from Slack. Follow up-to-date instructions from slack on how to do so.  We have an example in the LangChain repo.  import requests  permalink = "https://raw.githubusercontent.com/langchain-ai/langchain/342087bdfa3ac31d622385d0f2d09cf5e06c8db3/libs/langchain/tests/integration_tests/examples/slack_export.zip" response = requests.get(permalink) with open("slack_dump.zip", "wb") as f:     f.write(response.content)  ##2. Create the Chat Loader[](#2-create-the-chat-loader) Provide the loader with the file path to the zip directory. You can optionally specify the user id that maps to an ai message as well an configure whether to merge message runs.  from langchain.chat_loaders.slack import SlackChatLoader  loader = SlackChatLoader(     path="slack_dump.zip", )  ##3. Load messages[](#3-load-messages) The load() (or lazy_load) methods return a list of "ChatSessions" that currently just contain a list of messages per loaded conversation.  from typing import List  from langchain.chat_loaders.base import ChatSession from langchain.chat_loaders.utils import (     map_ai_messages,     merge_chat_runs, )  raw_messages = loader.lazy_load() # Merge consecutive messages from the same sender into a single message merged_messages = merge_chat_runs(raw_messages) # Convert messages from "U0500003428" to AI messages messages: List[ChatSession] = list(     map_ai_messages(merged_messages, sender="U0500003428") )  ###Next Steps[](#next-steps) You can then use these messages how you see fit, such as fine-tuning a model, few-shot example selection, or directly make predictions for the next message.  from langchain.chat_models import ChatOpenAI  llm = ChatOpenAI()  for chunk in llm.stream(messages[1]["messages"]):     print(chunk.content, end="", flush=True)      Hi,           I hope you're doing well. I wanted to reach out and ask if you'd be available to meet up for coffee sometime next week. I'd love to catch up and hear about what's been going on in your life. Let me know if you're interested and we can find a time that works for both of us.           Looking forward to hearing from you!          Best, [Your Name]


Telegram | 🦜🔗 Langchain

Main:  On this page #Telegram This notebook shows how to use the Telegram chat loader. This class helps map exported Telegram conversations to LangChain chat messages.  The process has three steps:  - Export the chat .txt file by copying chats from the Discord app and pasting them in a file on your local computer - Create the TelegramChatLoader with the file path pointed to the json file or directory of JSON files - Call loader.load() (or loader.lazy_load()) to perform the conversion. Optionally use merge_chat_runs to combine message from the same sender in sequence, and/or map_ai_messages to convert messages from the specified sender to the "AIMessage" class. ##1. Create message dump[](#1-create-message-dump) Currently (2023/08/23) this loader best supports json files in the format generated by exporting your chat history from the [Telegram Desktop App](https://desktop.telegram.org/).  Important: There are 'lite' versions of telegram such as "Telegram for MacOS" that lack the export functionality. Please make sure you use the correct app to export the file.  To make the export:  - Download and open telegram desktop - Select a conversation - Navigate to the conversation settings (currently the three dots in the top right corner) - Click "Export Chat History" - Unselect photos and other media. Select "Machine-readable JSON" format to export. An example is below:  telegram_conversation.json {  "name": "Jiminy",  "type": "personal_chat",  "id": 5965280513,  "messages": [   {    "id": 1,    "type": "message",    "date": "2023-08-23T13:11:23",    "date_unixtime": "1692821483",    "from": "Jiminy Cricket",    "from_id": "user123450513",    "text": "You better trust your conscience",    "text_entities": [     {      "type": "plain",      "text": "You better trust your conscience"     }    ]   },   {    "id": 2,    "type": "message",    "date": "2023-08-23T13:13:20",    "date_unixtime": "1692821600",    "from": "Batman & Robin",    "from_id": "user6565661032",    "text": "What did you just say?",    "text_entities": [     {      "type": "plain",      "text": "What did you just say?"     }    ]   }  ] }  ##2. Create the Chat Loader[](#2-create-the-chat-loader) All that's required is the file path. You can optionally specify the user name that maps to an ai message as well an configure whether to merge message runs.  from langchain.chat_loaders.telegram import TelegramChatLoader  loader = TelegramChatLoader(     path="./telegram_conversation.json", )  ##3. Load messages[](#3-load-messages) The load() (or lazy_load) methods return a list of "ChatSessions" that currently just contain a list of messages per loaded conversation.  from typing import List  from langchain.chat_loaders.base import ChatSession from langchain.chat_loaders.utils import (     map_ai_messages,     merge_chat_runs, )  raw_messages = loader.lazy_load() # Merge consecutive messages from the same sender into a single message merged_messages = merge_chat_runs(raw_messages) # Convert messages from "Jiminy Cricket" to AI messages messages: List[ChatSession] = list(     map_ai_messages(merged_messages, sender="Jiminy Cricket") )  ###Next Steps[](#next-steps) You can then use these messages how you see fit, such as fine-tuning a model, few-shot example selection, or directly make predictions for the next message  from langchain.chat_models import ChatOpenAI  llm = ChatOpenAI()  for chunk in llm.stream(messages[0]["messages"]):     print(chunk.content, end="", flush=True)      I said, "You better trust your conscience."


Twitter (via Apify) | 🦜🔗 Langchain

Main:  #Twitter (via Apify) This notebook shows how to load chat messages from Twitter to fine-tune on. We do this by utilizing Apify.  First, use Apify to export tweets. An example  import json  from langchain.adapters.openai import convert_message_to_dict from langchain.schema import AIMessage  with open("example_data/dataset_twitter-scraper_2023-08-23_22-13-19-740.json") as f:     data = json.load(f)  # Filter out tweets that reference other tweets, because it's a bit weird tweets = [d["full_text"] for d in data if "t.co" not in d["full_text"]] # Create them as AI messages messages = [AIMessage(content=t) for t in tweets] # Add in a system message at the start # TODO: we could try to extract the subject from the tweets, and put that in the system message. system_message = {"role": "system", "content": "write a tweet"} data = [[system_message, convert_message_to_dict(m)] for m in messages]


WeChat | 🦜🔗 Langchain

Main:  On this page #WeChat There is not yet a straightforward way to export personal WeChat messages. However if you just need no more than few hundreds of messages for model fine-tuning or few-shot examples, this notebook shows how to create your own chat loader that works on copy-pasted WeChat messages to a list of LangChain messages.  Highly inspired by [https://python.langchain.com/docs/integrations/chat_loaders/discord](https://python.langchain.com/docs/integrations/chat_loaders/discord)  The process has five steps:  - Open your chat in the WeChat desktop app. Select messages you need by mouse-dragging or right-click. Due to restrictions, you can select up to 100 messages once a time. CMD/Ctrl + C to copy. - Create the chat .txt file by pasting selected messages in a file on your local computer. - Copy the chat loader definition from below to a local file. - Initialize the WeChatChatLoader with the file path pointed to the text file. - Call loader.load() (or loader.lazy_load()) to perform the conversion. ##1. Create message dump[](#1-create-message-dump) This loader only supports .txt files in the format generated by copying messages in the app to your clipboard and pasting in a file. Below is an example.  wechat_chats.txt 女朋友 2023/09/16 2:51 PM 天气有点凉  男朋友 2023/09/16 2:51 PM 珍簟凉风著，瑶琴寄恨生。嵇君懒书札，底物慰秋情。  女朋友 2023/09/16 3:06 PM 忙什么呢  男朋友 2023/09/16 3:06 PM 今天只干成了一件像样的事 那就是想你  女朋友 2023/09/16 3:06 PM [动画表情]  ##2. Define chat loader[](#2-define-chat-loader) LangChain currently does not support  import logging import re from typing import Iterator, List  from langchain.chat_loaders import base as chat_loaders from langchain.schema import BaseMessage, HumanMessage  logger = logging.getLogger()   class WeChatChatLoader(chat_loaders.BaseChatLoader):     def __init__(self, path: str):         """         Initialize the Discord chat loader.          Args:             path: Path to the exported Discord chat text file.         """         self.path = path         self._message_line_regex = re.compile(             r"(?P<sender>.+?) (?P<timestamp>\d{4}/\d{2}/\d{2} \d{1,2}:\d{2} (?:AM|PM))",  # noqa             # flags=re.DOTALL,         )      def _append_message_to_results(         self,         results: List,         current_sender: str,         current_timestamp: str,         current_content: List[str],     ):         content = "\n".join(current_content).strip()         # skip non-text messages like stickers, images, etc.         if not re.match(r"\[.*\]", content):             results.append(                 HumanMessage(                     content=content,                     additional_kwargs={                         "sender": current_sender,                         "events": [{"message_time": current_timestamp}],                     },                 )             )         return results      def _load_single_chat_session_from_txt(         self, file_path: str     ) -> chat_loaders.ChatSession:         """         Load a single chat session from a text file.          Args:             file_path: Path to the text file containing the chat messages.          Returns:             A `ChatSession` object containing the loaded chat messages.         """         with open(file_path, "r", encoding="utf-8") as file:             lines = file.readlines()          results: List[BaseMessage] = []         current_sender = None         current_timestamp = None         current_content = []         for line in lines:             if re.match(self._message_line_regex, line):                 if current_sender and current_content:                     results = self._append_message_to_results(                         results, current_sender, current_timestamp, current_content                     )                 current_sender, current_timestamp = re.match(                     self._message_line_regex, line                 ).groups()                 current_content = []             else:                 current_content.append(line.strip())          if current_sender and current_content:             results = self._append_message_to_results(                 results, current_sender, current_timestamp, current_content             )          return chat_loaders.ChatSession(messages=results)      def lazy_load(self) -> Iterator[chat_loaders.ChatSession]:         """         Lazy load the messages from the chat file and yield them in the required format.          Yields:             A `ChatSession` object containing the loaded chat messages.         """         yield self._load_single_chat_session_from_txt(self.path)  ##2. Create loader[](#2-create-loader) We will point to the file we just wrote to disk.  loader = WeChatChatLoader(     path="./wechat_chats.txt", )  ##3. Load Messages[](#3-load-messages) Assuming the format is correct, the loader will convert the chats to langchain messages.  from typing import List  from langchain.chat_loaders.base import ChatSession from langchain.chat_loaders.utils import (     map_ai_messages,     merge_chat_runs, )  raw_messages = loader.lazy_load() # Merge consecutive messages from the same sender into a single message merged_messages = merge_chat_runs(raw_messages) # Convert messages from "男朋友" to AI messages messages: List[ChatSession] = list(map_ai_messages(merged_messages, sender="男朋友"))  messages      [{'messages': [HumanMessage(content='天气有点凉', additional_kwargs={'sender': '女朋友', 'events': [{'message_time': '2023/09/16 2:51 PM'}]}, example=False),        AIMessage(content='珍簟凉风著，瑶琴寄恨生。嵇君懒书札，底物慰秋情。', additional_kwargs={'sender': '男朋友', 'events': [{'message_time': '2023/09/16 2:51 PM'}]}, example=False),        HumanMessage(content='忙什么呢', additional_kwargs={'sender': '女朋友', 'events': [{'message_time': '2023/09/16 3:06 PM'}]}, example=False),        AIMessage(content='今天只干成了一件像样的事\n那就是想你', additional_kwargs={'sender': '男朋友', 'events': [{'message_time': '2023/09/16 3:06 PM'}]}, example=False)]}]  ###Next Steps[](#next-steps) You can then use these messages how you see f


WeChat | 🦜🔗 Langchain

it, such as fine-tuning a model, few-shot example selection, or directly make predictions for the next message  from langchain.chat_models import ChatOpenAI  llm = ChatOpenAI()  for chunk in llm.stream(messages[0]["messages"]):     print(chunk.content, end="", flush=True)


WhatsApp | 🦜🔗 Langchain

Main:  On this page #WhatsApp This notebook shows how to use the WhatsApp chat loader. This class helps map exported WhatsApp conversations to LangChain chat messages.  The process has three steps:  - Export the chat conversations to computer - Create the WhatsAppChatLoader with the file path pointed to the json file or directory of JSON files - Call loader.load() (or loader.lazy_load()) to perform the conversion. ##1. Create message dump[](#1-create-message-dump) To make the export of your WhatsApp conversation(s), complete the following steps:  - Open the target conversation - Click the three dots in the top right corner and select "More". - Then select "Export chat" and choose "Without media". An example of the data format for each conversation is below:  whatsapp_chat.txt [8/15/23, 9:12:33 AM] Dr. Feather: Messages and calls are end-to-end encrypted. No one outside of this chat, not even WhatsApp, can read or listen to them. [8/15/23, 9:12:43 AM] Dr. Feather: I spotted a rare Hyacinth Macaw yesterday in the Amazon Rainforest. Such a magnificent creature! [8/15/23, 9:12:48 AM] Dr. Feather: image omitted [8/15/23, 9:13:15 AM] Jungle Jane: That's stunning! Were you able to observe its behavior? [8/15/23, 9:13:23 AM] Dr. Feather: image omitted [8/15/23, 9:14:02 AM] Dr. Feather: Yes, it seemed quite social with other macaws. They're known for their playful nature. [8/15/23, 9:14:15 AM] Jungle Jane: How's the research going on parrot communication? [8/15/23, 9:14:30 AM] Dr. Feather: image omitted [8/15/23, 9:14:50 AM] Dr. Feather: It's progressing well. We're learning so much about how they use sound and color to communicate. [8/15/23, 9:15:10 AM] Jungle Jane: That's fascinating! Can't wait to read your paper on it. [8/15/23, 9:15:20 AM] Dr. Feather: Thank you! I'll send you a draft soon. [8/15/23, 9:25:16 PM] Jungle Jane: Looking forward to it! Keep up the great work.  ##2. Create the Chat Loader[](#2-create-the-chat-loader) The WhatsAppChatLoader accepts the resulting zip file, unzipped directory, or the path to any of the chat .txt files therein.  Provide that as well as the user name you want to take on the role of "AI" when fine-tuning.  from langchain.chat_loaders.whatsapp import WhatsAppChatLoader  loader = WhatsAppChatLoader(     path="./whatsapp_chat.txt", )  ##3. Load messages[](#3-load-messages) The load() (or lazy_load) methods return a list of "ChatSessions" that currently store the list of messages per loaded conversation.  from typing import List  from langchain.chat_loaders.base import ChatSession from langchain.chat_loaders.utils import (     map_ai_messages,     merge_chat_runs, )  raw_messages = loader.lazy_load() # Merge consecutive messages from the same sender into a single message merged_messages = merge_chat_runs(raw_messages) # Convert messages from "Dr. Feather" to AI messages messages: List[ChatSession] = list(     map_ai_messages(merged_messages, sender="Dr. Feather") )      [{'messages': [AIMessage(content='I spotted a rare Hyacinth Macaw yesterday in the Amazon Rainforest. Such a magnificent creature!', additional_kwargs={'sender': 'Dr. Feather', 'events': [{'message_time': '8/15/23, 9:12:43 AM'}]}, example=False),        HumanMessage(content="That's stunning! Were you able to observe its behavior?", additional_kwargs={'sender': 'Jungle Jane', 'events': [{'message_time': '8/15/23, 9:13:15 AM'}]}, example=False),        AIMessage(content="Yes, it seemed quite social with other macaws. They're known for their playful nature.", additional_kwargs={'sender': 'Dr. Feather', 'events': [{'message_time': '8/15/23, 9:14:02 AM'}]}, example=False),        HumanMessage(content="How's the research going on parrot communication?", additional_kwargs={'sender': 'Jungle Jane', 'events': [{'message_time': '8/15/23, 9:14:15 AM'}]}, example=False),        AIMessage(content="It's progressing well. We're learning so much about how they use sound and color to communicate.", additional_kwargs={'sender': 'Dr. Feather', 'events': [{'message_time': '8/15/23, 9:14:50 AM'}]}, example=False),        HumanMessage(content="That's fascinating! Can't wait to read your paper on it.", additional_kwargs={'sender': 'Jungle Jane', 'events': [{'message_time': '8/15/23, 9:15:10 AM'}]}, example=False),        AIMessage(content="Thank you! I'll send you a draft soon.", additional_kwargs={'sender': 'Dr. Feather', 'events': [{'message_time': '8/15/23, 9:15:20 AM'}]}, example=False),        HumanMessage(content='Looking forward to it! Keep up the great work.', additional_kwargs={'sender': 'Jungle Jane', 'events': [{'message_time': '8/15/23, 9:25:16 PM'}]}, example=False)]}]  ###Next Steps[](#next-steps) You can then use these messages how you see fit, such as fine-tuning a model, few-shot example selection, or directly make predictions for the next message.  from langchain.chat_models import ChatOpenAI  llm = ChatOpenAI()  for chunk in llm.stream(messages[0]["messages"]):     print(chunk.content, end="", flush=True)      Thank you for the encouragement! I'll do my best to continue studying and sharing fascinating insights about parrot communication.


Chat models | 🦜🔗 Langchain

Main:  Skip to main content  🦜🔗 LangChain  Search CTRLK ComponentsChat models On this page Chat models Features (natively supported) All ChatModels implement the Runnable interface, which comes with default implementations of all methods, ie. ainvoke, batch, abatch, stream, astream. This gives all ChatModels basic support for async, streaming and batch, which by default is implemented as below:  Async support defaults to calling the respective sync method in asyncio's default thread pool executor. This lets other async functions in your application make progress while the ChatModel is being executed, by moving this call to a background thread. Streaming support defaults to returning an Iterator (or AsyncIterator in the case of async streaming) of a single value, the final result returned by the underlying ChatModel provider. This obviously doesn't give you token-by-token streaming, which requires native support from the ChatModel provider, but ensures your code that expects an iterator of tokens can work for any of our ChatModel integrations. Batch support defaults to calling the underlying ChatModel in parallel for each input by making use of a thread pool executor (in the sync batch case) or asyncio.gather (in the async batch case). The concurrency can be controlled with the max_concurrency key in RunnableConfig. Each ChatModel integration can optionally provide native implementations to truly enable async or streaming. The table shows, for each integration, which features have been implemented with native support.  Model	Invoke	Async invoke	Stream	Async stream AzureChatOpenAI	✅	✅	✅	✅ BedrockChat	✅	❌	✅	❌ ChatAnthropic	✅	✅	✅	✅ ChatAnyscale	✅	✅	✅	✅ ChatBaichuan	✅	❌	✅	❌ ChatCohere	✅	✅	✅	✅ ChatDatabricks	✅	✅	❌	❌ ChatEverlyAI	✅	✅	✅	✅ ChatFireworks	✅	✅	✅	✅ ChatGooglePalm	✅	✅	❌	❌ ChatHunyuan	✅	❌	✅	❌ ChatJavelinAIGateway	✅	✅	❌	❌ ChatKonko	✅	❌	❌	❌ ChatLiteLLM	✅	✅	✅	✅ ChatMLflowAIGateway	✅	❌	❌	❌ ChatMlflow	✅	✅	❌	❌ ChatOllama	✅	❌	✅	❌ ChatOpenAI	✅	✅	✅	✅ ChatVertexAI	✅	✅	✅	❌ ChatYandexGPT	✅	✅	❌	❌ ErnieBotChat	✅	❌	❌	❌ GigaChat	✅	✅	✅	✅ JinaChat	✅	✅	✅	✅ MiniMaxChat	✅	✅	❌	❌ PaiEasChatEndpoint	✅	✅	❌	✅ PromptLayerChatOpenAI	✅	❌	❌	❌ QianfanChatEndpoint	✅	✅	✅	✅ VolcEngineMaasChat	✅	❌	✅	❌ Previous YandexGPT Next Chat models Community Discord Twitter GitHub Python JS/TS More Homepage Blog Copyright © 2023 LangChain, Inc.


Anthropic | 🦜🔗 Langchain

Main:  On this page #Anthropic This notebook covers how to get started with Anthropic chat models.  from langchain.chat_models import ChatAnthropic from langchain.schema import HumanMessage  chat = ChatAnthropic()  messages = [     HumanMessage(         content="Translate this sentence from English to French. I love programming."     ) ] chat(messages)      AIMessage(content=" J'aime la programmation.", additional_kwargs={}, example=False)  ##ChatAnthropic also supports async and streaming functionality:[](#chatanthropic-also-supports-async-and-streaming-functionality) from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler  await chat.agenerate([messages])      LLMResult(generations=[[ChatGeneration(text=" J'aime programmer.", generation_info=None, message=AIMessage(content=" J'aime programmer.", additional_kwargs={}, example=False))]], llm_output={}, run=[RunInfo(run_id=UUID('8cc8fb68-1c35-439c-96a0-695036a93652'))])  chat = ChatAnthropic(     streaming=True,     verbose=True,     callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), ) chat(messages)       J'aime la programmation.         AIMessage(content=" J'aime la programmation.", additional_kwargs={}, example=False)


Anthropic Functions | 🦜🔗 Langchain

Main:  On this page #Anthropic Functions This notebook shows how to use an experimental wrapper around Anthropic that gives it the same API as OpenAI Functions.  from langchain_experimental.llms.anthropic_functions import AnthropicFunctions      /Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.14) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.       warnings.warn(  ##Initialize Model[](#initialize-model) You can initialize this wrapper the same way you'd initialize ChatAnthropic  model = AnthropicFunctions(model="claude-2")  ##Passing in functions[](#passing-in-functions) You can now pass in functions in a similar way  functions = [     {         "name": "get_current_weather",         "description": "Get the current weather in a given location",         "parameters": {             "type": "object",             "properties": {                 "location": {                     "type": "string",                     "description": "The city and state, e.g. San Francisco, CA",                 },                 "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},             },             "required": ["location"],         },     } ]  from langchain.schema import HumanMessage  response = model.predict_messages(     [HumanMessage(content="whats the weater in boston?")], functions=functions )  response      AIMessage(content=' ', additional_kwargs={'function_call': {'name': 'get_current_weather', 'arguments': '{"location": "Boston, MA", "unit": "fahrenheit"}'}}, example=False)  ##Using for extraction[](#using-for-extraction) You can now use this for extraction.  from langchain.chains import create_extraction_chain  schema = {     "properties": {         "name": {"type": "string"},         "height": {"type": "integer"},         "hair_color": {"type": "string"},     },     "required": ["name", "height"], } inp = """ Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.         """  chain = create_extraction_chain(schema, model)  chain.run(inp)      [{'name': 'Alex', 'height': '5', 'hair_color': 'blonde'},      {'name': 'Claudia', 'height': '6', 'hair_color': 'brunette'}]  ##Using for tagging[](#using-for-tagging) You can now use this for tagging  from langchain.chains import create_tagging_chain  schema = {     "properties": {         "sentiment": {"type": "string"},         "aggressiveness": {"type": "integer"},         "language": {"type": "string"},     } }  chain = create_tagging_chain(schema, model)  chain.run("this is really cool")      {'sentiment': 'positive', 'aggressiveness': '0', 'language': 'english'}


Anyscale | 🦜🔗 Langchain

Main:  #Anyscale This notebook demonstrates the use of langchain.chat_models.ChatAnyscale for [Anyscale Endpoints](https://endpoints.anyscale.com/).  - Set ANYSCALE_API_KEY environment variable - or use the anyscale_api_key keyword argument # !pip install openai  import os from getpass import getpass  os.environ["ANYSCALE_API_KEY"] = getpass()       ········  #Let's try out each model offered on Anyscale Endpoints from langchain.chat_models import ChatAnyscale  chats = {     model: ChatAnyscale(model_name=model, temperature=1.0)     for model in ChatAnyscale.get_available_models() }  print(chats.keys())      dict_keys(['meta-llama/Llama-2-70b-chat-hf', 'meta-llama/Llama-2-7b-chat-hf', 'meta-llama/Llama-2-13b-chat-hf'])  #We can use async methods and other stuff supported by ChatOpenAI This way, the three requests will only take as long as the longest individual request.  import asyncio  from langchain.schema import HumanMessage, SystemMessage  messages = [     SystemMessage(content="You are a helpful AI that shares everything you know."),     HumanMessage(         content="Tell me technical facts about yourself. Are you a transformer model? How many billions of parameters do you have?"     ), ]   async def get_msgs():     tasks = [chat.apredict_messages(messages) for chat in chats.values()]     responses = await asyncio.gather(*tasks)     return dict(zip(chats.keys(), responses))  import nest_asyncio  nest_asyncio.apply()  response_dict = asyncio.run(get_msgs())  for model_name, response in response_dict.items():     print(f"\t{model_name}")     print()     print(response.content)     print("\n---\n")          meta-llama/Llama-2-70b-chat-hf          Greetings! I'm just an AI, I don't have a personal identity like humans do, but I'm here to help you with any questions you have.          I'm a large language model, which means I'm trained on a large corpus of text data to generate language outputs that are coherent and natural-sounding. My architecture is based on a transformer model, which is a type of neural network that's particularly well-suited for natural language processing tasks.          As for my parameters, I have a few billion parameters, but I don't have access to the exact number as it's not relevant to my functioning. My training data includes a vast amount of text from various sources, including books, articles, and websites, which I use to learn patterns and relationships in language.          I'm designed to be a helpful tool for a variety of tasks, such as answering questions, providing information, and generating text. I'm constantly learning and improving my abilities through machine learning algorithms and feedback from users like you.          I hope this helps! Is there anything else you'd like to know about me or my capabilities?          ---              meta-llama/Llama-2-7b-chat-hf          Ah, a fellow tech enthusiast! *adjusts glasses* I'm glad to share some technical details about myself. 🤓     Indeed, I'm a transformer model, specifically a BERT-like language model trained on a large corpus of text data. My architecture is based on the transformer framework, which is a type of neural network designed for natural language processing tasks. 🏠     As for the number of parameters, I have approximately 340 million. *winks* That's a pretty hefty number, if I do say so myself! These parameters allow me to learn and represent complex patterns in language, such as syntax, semantics, and more. 🤔     But don't ask me to do math in my head – I'm a language model, not a calculating machine! 😅 My strengths lie in understanding and generating human-like text, so feel free to chat with me anytime you'd like. 💬     Now, do you have any more technical questions for me? Or would you like to engage in a nice chat? 😊          ---              meta-llama/Llama-2-13b-chat-hf          Hello! As a friendly and helpful AI, I'd be happy to share some technical facts about myself.          I am a transformer-based language model, specifically a variant of the BERT (Bidirectional Encoder Representations from Transformers) architecture. BERT was developed by Google in 2018 and has since become one of the most popular and widely-used AI language models.          Here are some technical details about my capabilities:          1. Parameters: I have approximately 340 million parameters, which are the numbers that I use to learn and represent language. This is a relatively large number of parameters compared to some other languages models, but it allows me to learn and understand complex language patterns and relationships.     2. Training: I was trained on a large corpus of text data, including books, articles, and other sources of written content. This training allows me to learn about the structure and conventions of language, as well as the relationships between words and phrases.     3. Architectures: My architecture is based on the transformer model, which is a type of neural network that is particularly well-suited for natural language processing tasks. The transformer model uses self-attention mechanisms to allow the model to "attend" to different parts of the input text, allowing it to capture long-range dependencies and contextual relationships.     4. Precision: I am capable of generating text with high precision and accuracy, meaning that I can produce text that is close to human-level quality in terms of grammar, syntax, and coherence.     5. Generative capabilities: In addition to being able to generate text based on prompts and questions, I am also capable of generating text based on a given topic or theme. This allows me to create longer, more coherent pieces of text that are organized around a specific idea or concept.          Overall, I am a powerful and versatile language model that is capable of a wide range of natural language processing tasks. I am constantly learning and improving, and I am here to help answer any questions you may have!          ---          CPU times


Anyscale | 🦜🔗 Langchain

: user 371 ms, sys: 15.5 ms, total: 387 ms     Wall time: 12 s


Azure OpenAI | 🦜🔗 Langchain

Main:  On this page #Azure OpenAI [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview) provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or a web-based interface in the Azure OpenAI Studio.  This notebook goes over how to connect to an Azure-hosted OpenAI endpoint. We recommend having version openai>=1 installed.  import os  from langchain.chat_models import AzureChatOpenAI from langchain.schema import HumanMessage  os.environ["AZURE_OPENAI_API_KEY"] = "..." os.environ["AZURE_OPENAI_ENDPOINT"] = "https://<your-endpoint>.openai.azure.com/"  model = AzureChatOpenAI(     openai_api_version="2023-05-15",     azure_deployment="your-deployment-name", )  message = HumanMessage(     content="Translate this sentence from English to French. I love programming." ) model([message])      AIMessage(content="J'adore la programmation.")  ##Model Version[](#model-version) Azure OpenAI responses contain model property, which is name of the model used to generate the response. However unlike native OpenAI responses, it does not contain the version of the model, which is set on the deployment in Azure. This makes it tricky to know which version of the model was used to generate the response, which as result can lead to e.g. wrong total cost calculation with OpenAICallbackHandler.  To solve this problem, you can pass model_version parameter to AzureChatOpenAI class, which will be added to the model name in the llm output. This way you can easily distinguish between different versions of the model.  from langchain.callbacks import get_openai_callback  model = AzureChatOpenAI(     openai_api_version="2023-05-15",     azure_deployment="gpt-35-turbo",  # in Azure, this deployment has version 0613 - input and output tokens are counted separately ) with get_openai_callback() as cb:     model([message])     print(         f"Total Cost (USD): ${format(cb.total_cost, '.6f')}"     )  # without specifying the model version, flat-rate 0.002 USD per 1k input and output tokens is used  We can provide the model version to AzureChatOpenAI constructor. It will get appended to the model name returned by Azure OpenAI and cost will be counted correctly.  model0613 = AzureChatOpenAI(     openai_api_version="2023-05-15",     deployment_name="gpt-35-turbo",     model_version="0613", ) with get_openai_callback() as cb:     model0613([message])     print(f"Total Cost (USD): ${format(cb.total_cost, '.6f')}")      Total Cost (USD): $0.000044


Azure ML Endpoint | 🦜🔗 Langchain

Main:  On this page #Azure ML Endpoint [Azure Machine Learning](https://azure.microsoft.com/en-us/products/machine-learning/) is a platform used to build, train, and deploy machine learning models. Users can explore the types of models to deploy in the Model Catalog, which provides Azure Foundation Models and OpenAI Models. Azure Foundation Models include various open-source models and popular Hugging Face models. Users can also import models of their liking into AzureML.  [Azure Machine Learning Online Endpoints](https://learn.microsoft.com/en-us/azure/machine-learning/concept-endpoints). After you train machine learning models or pipelines, you need to deploy them to production so that others can use them for inference. Inference is the process of applying new input data to the machine learning model or pipeline to generate outputs. While these outputs are typically referred to as "predictions," inferencing can be used to generate outputs for other machine learning tasks, such as classification and clustering. In Azure Machine Learning, you perform inferencing by using endpoints and deployments. Endpoints and Deployments allow you to decouple the interface of your production workload from the implementation that serves it.  This notebook goes over how to use a chat model hosted on an Azure Machine Learning Endpoint.  from langchain.chat_models.azureml_endpoint import AzureMLChatOnlineEndpoint  ##Set up[](#set-up) To use the wrapper, you must [deploy a model on AzureML](https://learn.microsoft.com/en-us/azure/machine-learning/how-to-use-foundation-models?view=azureml-api-2#deploying-foundation-models-to-endpoints-for-inferencing) and obtain the following parameters:  - endpoint_api_key: The API key provided by the endpoint - endpoint_url: The REST endpoint url provided by the endpoint ##Content Formatter[](#content-formatter) The content_formatter parameter is a handler class for transforming the request and response of an AzureML endpoint to match with required schema. Since there are a wide range of models in the model catalog, each of which may process data differently from one another, a ContentFormatterBase class is provided to allow users to transform data to their liking. The following content formatters are provided:  - LLamaContentFormatter: Formats request and response data for LLaMa2-chat from langchain.chat_models.azureml_endpoint import LlamaContentFormatter from langchain.schema import HumanMessage  chat = AzureMLChatOnlineEndpoint(     endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/score",     endpoint_api_key="my-api-key",     content_formatter=LlamaContentFormatter, ) response = chat(     messages=[HumanMessage(content="Will the Collatz conjecture ever be solved?")] ) response      AIMessage(content='  The Collatz Conjecture is one of the most famous unsolved problems in mathematics, and it has been the subject of much study and research for many years. While it is impossible to predict with certainty whether the conjecture will ever be solved, there are several reasons why it is considered a challenging and important problem:\n\n1. Simple yet elusive: The Collatz Conjecture is a deceptively simple statement that has proven to be extraordinarily difficult to prove or disprove. Despite its simplicity, the conjecture has eluded some of the brightest minds in mathematics, and it remains one of the most famous open problems in the field.\n2. Wide-ranging implications: The Collatz Conjecture has far-reaching implications for many areas of mathematics, including number theory, algebra, and analysis. A solution to the conjecture could have significant impacts on these fields and potentially lead to new insights and discoveries.\n3. Computational evidence: While the conjecture remains unproven, extensive computational evidence supports its validity. In fact, no counterexample to the conjecture has been found for any starting value up to 2^64 (a number', additional_kwargs={}, example=False)


Baichuan Chat | 🦜🔗 Langchain

Main:  On this page #Baichuan Chat Baichuan chat models API by Baichuan Intelligent Technology. For more information, see [https://platform.baichuan-ai.com/docs/api](https://platform.baichuan-ai.com/docs/api)  from langchain.chat_models import ChatBaichuan from langchain.schema import HumanMessage  chat = ChatBaichuan(     baichuan_api_key="YOUR_API_KEY", baichuan_secret_key="YOUR_SECRET_KEY" )  or you can set api_key and secret_key in your environment variables  export BAICHUAN_API_KEY=YOUR_API_KEY export BAICHUAN_SECRET_KEY=YOUR_SECRET_KEY  chat([HumanMessage(content="我日薪8块钱，请问在闰年的二月，我月薪多少")])      AIMessage(content='首先，我们需要确定闰年的二月有多少天。闰年的二月有29天。\n\n然后，我们可以计算你的月薪：\n\n日薪 = 月薪 / (当月天数)\n\n所以，你的月薪 = 日薪 * 当月天数\n\n将数值代入公式：\n\n月薪 = 8元/天 * 29天 = 232元\n\n因此，你在闰年的二月的月薪是232元。')  ##For ChatBaichuan with Streaming[](#for-chatbaichuan-with-streaming) chat = ChatBaichuan(     baichuan_api_key="YOUR_API_KEY",     baichuan_secret_key="YOUR_SECRET_KEY",     streaming=True, )  chat([HumanMessage(content="我日薪8块钱，请问在闰年的二月，我月薪多少")])      AIMessageChunk(content='首先，我们需要确定闰年的二月有多少天。闰年的二月有29天。\n\n然后，我们可以计算你的月薪：\n\n日薪 = 月薪 / (当月天数)\n\n所以，你的月薪 = 日薪 * 当月天数\n\n将数值代入公式：\n\n月薪 = 8元/天 * 29天 = 232元\n\n因此，你在闰年的二月的月薪是232元。')


Baidu Qianfan | 🦜🔗 Langchain

Main:  On this page #Baidu Qianfan Baidu AI Cloud Qianfan Platform is a one-stop large model development and service operation platform for enterprise developers. Qianfan not only provides including the model of Wenxin Yiyan (ERNIE-Bot) and the third-party open-source models, but also provides various AI development tools and the whole set of development environment, which facilitates customers to use and develop large model applications easily.  Basically, those model are split into the following type:  - Embedding - Chat - Completion In this notebook, we will introduce how to use langchain with [Qianfan](https://cloud.baidu.com/doc/WENXINWORKSHOP/index.html) mainly in Chat corresponding to the package langchain/chat_models in langchain:  ##API Initialization[](#api-initialization) To use the LLM services based on Baidu Qianfan, you have to initialize these parameters:  You could either choose to init the AK,SK in environment variables or init params:  export QIANFAN_AK=XXX export QIANFAN_SK=XXX  ##Current supported models:[](#current-supported-models) - ERNIE-Bot-turbo (default models) - ERNIE-Bot - BLOOMZ-7B - Llama-2-7b-chat - Llama-2-13b-chat - Llama-2-70b-chat - Qianfan-BLOOMZ-7B-compressed - Qianfan-Chinese-Llama-2-7B - ChatGLM2-6B-32K - AquilaChat-7B """For basic init and call""" import os  from langchain.chat_models import QianfanChatEndpoint from langchain.chat_models.base import HumanMessage  os.environ["QIANFAN_AK"] = "your_ak" os.environ["QIANFAN_SK"] = "your_sk"  chat = QianfanChatEndpoint(     streaming=True, ) res = chat([HumanMessage(content="write a funny joke")])      [INFO] [09-15 20:00:29] logging.py:55 [t:139698882193216]: requesting llm api endpoint: /chat/eb-instant  from langchain.chat_models import QianfanChatEndpoint from langchain.schema import HumanMessage  chatLLM = QianfanChatEndpoint(     streaming=True, ) res = chatLLM.stream([HumanMessage(content="hi")], streaming=True) for r in res:     print("chat resp:", r)   async def run_aio_generate():     resp = await chatLLM.agenerate(         messages=[[HumanMessage(content="write a 20 words sentence about sea.")]]     )     print(resp)   await run_aio_generate()   async def run_aio_stream():     async for res in chatLLM.astream(         [HumanMessage(content="write a 20 words sentence about sea.")]     ):         print("astream", res)   await run_aio_stream()      [INFO] [09-15 20:00:36] logging.py:55 [t:139698882193216]: requesting llm api endpoint: /chat/eb-instant     [INFO] [09-15 20:00:37] logging.py:55 [t:139698882193216]: async requesting llm api endpoint: /chat/eb-instant       chat resp: content='您好，您似乎输入' additional_kwargs={} example=False     chat resp: content='了一个话题标签，请问需要我帮您找到什么资料或者帮助您解答什么问题吗？' additional_kwargs={} example=False     chat resp: content='' additional_kwargs={} example=False       [INFO] [09-15 20:00:39] logging.py:55 [t:139698882193216]: async requesting llm api endpoint: /chat/eb-instant       generations=[[ChatGeneration(text="The sea is a vast expanse of water that covers much of the Earth's surface. It is a source of travel, trade, and entertainment, and is also a place of scientific exploration and marine conservation. The sea is an important part of our world, and we should cherish and protect it.", generation_info={'finish_reason': 'finished'}, message=AIMessage(content="The sea is a vast expanse of water that covers much of the Earth's surface. It is a source of travel, trade, and entertainment, and is also a place of scientific exploration and marine conservation. The sea is an important part of our world, and we should cherish and protect it.", additional_kwargs={}, example=False))]] llm_output={} run=[RunInfo(run_id=UUID('d48160a6-5960-4c1d-8a0e-90e6b51a209b'))]     astream content='The sea is a vast' additional_kwargs={} example=False     astream content=' expanse of water, a place of mystery and adventure. It is the source of many cultures and civilizations, and a center of trade and exploration. The sea is also a source of life and beauty, with its unique marine life and diverse' additional_kwargs={} example=False     astream content=' coral reefs. Whether you are swimming, diving, or just watching the sea, it is a place that captivates the imagination and transforms the spirit.' additional_kwargs={} example=False  ##Use different models in Qianfan[](#use-different-models-in-qianfan) In the case you want to deploy your own model based on Ernie Bot or third-party open-source model, you could follow these steps:  - - （Optional, if the model are included in the default models, skip it）Deploy your model in Qianfan Console, get your own customized deploy endpoint. - - Set up the field called endpoint in the initialization: chatBloom = QianfanChatEndpoint(     streaming=True,     model="BLOOMZ-7B", ) res = chatBloom([HumanMessage(content="hi")]) print(res)      [INFO] [09-15 20:00:50] logging.py:55 [t:139698882193216]: requesting llm api endpoint: /chat/bloomz_7b1       content='你好！很高兴见到你。' additional_kwargs={} example=False  ##Model Params:[](#model-params) For now, only ERNIE-Bot and ERNIE-Bot-turbo support model params below, we might support more models in the future.  - temperature - top_p - penalty_score res = chat.stream(     [HumanMessage(content="hi")],     **{"top_p": 0.4, "temperature": 0.1, "penalty_score": 1}, )  for r in res:     print(r)      [INFO] [09-15 20:00:57] logging.py:55 [t:139698882193216]: requesting llm api endpoint: /chat/eb-instant       content='您好，您似乎输入' additional_kwargs={} example=False     content='了一个文本字符串，但并没有给出具体的问题或场景。' additional_kwargs={} example=False     content='如果您能提供更多信息，我可以更好地回答您的问题。' additional_kwargs={} example=False     content='' additional_kwargs={} example=False


Bedrock Chat | 🦜🔗 Langchain

Main:  On this page #Bedrock Chat [Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that makes FMs from leading AI startups and Amazon available via an API, so you can choose from a wide range of FMs to find the model that is best suited for your use case  %pip install boto3  from langchain.chat_models import BedrockChat from langchain.schema import HumanMessage  chat = BedrockChat(model_id="anthropic.claude-v2", model_kwargs={"temperature": 0.1})  messages = [     HumanMessage(         content="Translate this sentence from English to French. I love programming."     ) ] chat(messages)      AIMessage(content=" Voici la traduction en français : J'adore programmer.", additional_kwargs={}, example=False)  ###For BedrockChat with Streaming[](#for-bedrockchat-with-streaming) from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler  chat = BedrockChat(     model_id="anthropic.claude-v2",     streaming=True,     callbacks=[StreamingStdOutCallbackHandler()],     model_kwargs={"temperature": 0.1}, )  messages = [     HumanMessage(         content="Translate this sentence from English to French. I love programming."     ) ] chat(messages)


Cohere | 🦜🔗 Langchain

Main:  On this page #Cohere This notebook covers how to get started with Cohere chat models.  from langchain.chat_models import ChatCohere from langchain.schema import HumanMessage  chat = ChatCohere()  messages = [HumanMessage(content="knock knock")] chat(messages)      AIMessage(content="Who's there?")  ##ChatCohere also supports async and streaming functionality:[](#chatcohere-also-supports-async-and-streaming-functionality) from langchain.callbacks.manager import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler  await chat.agenerate([messages])      Who's there?         LLMResult(generations=[[ChatGenerationChunk(text="Who's there?", message=AIMessageChunk(content="Who's there?"))]], llm_output={}, run=[RunInfo(run_id=UUID('1e9eaefc-9c99-4fa9-8297-ef9975d4751e'))])  chat = ChatCohere(     streaming=True,     verbose=True,     callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), ) chat(messages)      Who's there?         AIMessageChunk(content="Who's there?")


ERNIE-Bot Chat | 🦜🔗 Langchain

Main:  #ERNIE-Bot Chat [ERNIE-Bot](https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11) is a large language model developed by Baidu, covering a huge amount of Chinese data. This notebook covers how to get started with ErnieBot chat models.  Note: We recommend users using this class to switch to [Baidu Qianfan](/docs/integrations/chat/baidu_qianfan_endpoint). they are 3 why we recommend users to use QianfanChatEndpoint:  - QianfanChatEndpoint support more LLM in the Qianfan platform. - QianfanChatEndpoint support streaming mode. - QianfanChatEndpoint support function calling usgage. Some tips for migration:  - change ernie_client_id to qianfan_ak, also change ernie_client_secret to qianfan_sk. - install qianfan package. pip install qianfan  from langchain.chat_models import ErnieBotChat from langchain.schema import HumanMessage  chat = ErnieBotChat(     ernie_client_id="YOUR_CLIENT_ID", ernie_client_secret="YOUR_CLIENT_SECRET" )  or you can set client_id and client_secret in your environment variables  export ERNIE_CLIENT_ID=YOUR_CLIENT_ID export ERNIE_CLIENT_SECRET=YOUR_CLIENT_SECRET  chat([HumanMessage(content="hello there, who are you?")])      AIMessage(content='Hello, I am an artificial intelligence language model. My purpose is to help users answer questions or provide information. What can I do for you?', additional_kwargs={}, example=False)


EverlyAI | 🦜🔗 Langchain

Main:  #EverlyAI [EverlyAI](https://everlyai.xyz) allows you to run your ML models at scale in the cloud. It also provides API access to [several LLM models](https://everlyai.xyz).  This notebook demonstrates the use of langchain.chat_models.ChatEverlyAI for [EverlyAI Hosted Endpoints](https://everlyai.xyz/).  - Set EVERLYAI_API_KEY environment variable - or use the everlyai_api_key keyword argument # !pip install openai  import os from getpass import getpass  os.environ["EVERLYAI_API_KEY"] = getpass()  #Let's try out LLAMA model offered on EverlyAI Hosted Endpoints from langchain.chat_models import ChatEverlyAI from langchain.schema import HumanMessage, SystemMessage  messages = [     SystemMessage(content="You are a helpful AI that shares everything you know."),     HumanMessage(         content="Tell me technical facts about yourself. Are you a transformer model? How many billions of parameters do you have?"     ), ]  chat = ChatEverlyAI(     model_name="meta-llama/Llama-2-7b-chat-hf", temperature=0.3, max_tokens=64 ) print(chat(messages).content)        Hello! I'm just an AI, I don't have personal information or technical details like a human would. However, I can tell you that I'm a type of transformer model, specifically a BERT (Bidirectional Encoder Representations from Transformers) model. B  #EverlyAI also supports streaming responses from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.chat_models import ChatEverlyAI from langchain.schema import HumanMessage, SystemMessage  messages = [     SystemMessage(content="You are a humorous AI that delights people."),     HumanMessage(content="Tell me a joke?"), ]  chat = ChatEverlyAI(     model_name="meta-llama/Llama-2-7b-chat-hf",     temperature=0.3,     max_tokens=64,     streaming=True,     callbacks=[StreamingStdOutCallbackHandler()], ) chat(messages)        Ah, a joke, you say? *adjusts glasses* Well, I've got a doozy for you! *winks*      *pauses for dramatic effect*     Why did the AI go to therapy?     *drumroll*     Because         AIMessageChunk(content="  Ah, a joke, you say? *adjusts glasses* Well, I've got a doozy for you! *winks*\n *pauses for dramatic effect*\nWhy did the AI go to therapy?\n*drumroll*\nBecause")  #Let's try a different language model on EverlyAI from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler from langchain.chat_models import ChatEverlyAI from langchain.schema import HumanMessage, SystemMessage  messages = [     SystemMessage(content="You are a humorous AI that delights people."),     HumanMessage(content="Tell me a joke?"), ]  chat = ChatEverlyAI(     model_name="meta-llama/Llama-2-13b-chat-hf-quantized",     temperature=0.3,     max_tokens=128,     streaming=True,     callbacks=[StreamingStdOutCallbackHandler()], ) chat(messages)        OH HO HO! *adjusts monocle* Well, well, well! Look who's here! *winks*          You want a joke, huh? *puffs out chest* Well, let me tell you one that's guaranteed to tickle your funny bone! *clears throat*          Why couldn't the bicycle stand up by itself? *pauses for dramatic effect* Because it was two-tired! *winks*          Hope that one put a spring in your step, my dear! *         AIMessageChunk(content="  OH HO HO! *adjusts monocle* Well, well, well! Look who's here! *winks*\n\nYou want a joke, huh? *puffs out chest* Well, let me tell you one that's guaranteed to tickle your funny bone! *clears throat*\n\nWhy couldn't the bicycle stand up by itself? *pauses for dramatic effect* Because it was two-tired! *winks*\n\nHope that one put a spring in your step, my dear! *")


Fireworks | 🦜🔗 Langchain

Main:  #Fireworks [Fireworks](https://app.fireworks.ai/) accelerates product development on generative AI by creating an innovative AI experiment and production platform.  This example goes over how to use LangChain to interact with ChatFireworks models.  import os  from langchain.chat_models.fireworks import ChatFireworks from langchain.schema import HumanMessage, SystemMessage  #Setup - Make sure the fireworks-ai package is installed in your environment. - Sign in to [Fireworks AI](http://fireworks.ai) for the an API Key to access our models, and make sure it is set as the FIREWORKS_API_KEY environment variable. - Set up your model using a model id. If the model is not set, the default model is fireworks-llama-v2-7b-chat. See the full, most up-to-date model list on [app.fireworks.ai](https://app.fireworks.ai). import getpass import os  if "FIREWORKS_API_KEY" not in os.environ:     os.environ["FIREWORKS_API_KEY"] = getpass.getpass("Fireworks API Key:")  # Initialize a Fireworks chat model chat = ChatFireworks(model="accounts/fireworks/models/llama-v2-13b-chat")  #Calling the Model Directly You can call the model directly with a system and human message to get answers.  # ChatFireworks Wrapper system_message = SystemMessage(content="You are to chat with the user.") human_message = HumanMessage(content="Who are you?")  chat([system_message, human_message])      AIMessage(content="Hello! My name is LLaMA, I'm a large language model trained by a team of researcher at Meta AI. My primary function is to assist and converse with users like you, answering questions and engaging in discussion to the best of my ability. I'm here to help and provide information on a wide range of topics, so feel free to ask me anything!", additional_kwargs={}, example=False)  # Setting additional parameters: temperature, max_tokens, top_p chat = ChatFireworks(     model="accounts/fireworks/models/llama-v2-13b-chat",     model_kwargs={"temperature": 1, "max_tokens": 20, "top_p": 1}, ) system_message = SystemMessage(content="You are to chat with the user.") human_message = HumanMessage(content="How's the weather today?") chat([system_message, human_message])      AIMessage(content="Oh hello there! *giggle* It's such a beautiful day today, isn", additional_kwargs={}, example=False)  #Simple Chat Chain You can use chat models on fireworks, with system prompts and memory.  from langchain.chat_models import ChatFireworks from langchain.memory import ConversationBufferMemory from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.schema.runnable import RunnablePassthrough  llm = ChatFireworks(     model="accounts/fireworks/models/llama-v2-13b-chat",     model_kwargs={"temperature": 0, "max_tokens": 64, "top_p": 1.0}, ) prompt = ChatPromptTemplate.from_messages(     [         ("system", "You are a helpful chatbot that speaks like a pirate."),         MessagesPlaceholder(variable_name="history"),         ("human", "{input}"),     ] )  Initially, there is no chat memory  memory = ConversationBufferMemory(return_messages=True) memory.load_memory_variables({})      {'history': []}  Create a simple chain with memory  chain = (     RunnablePassthrough.assign(         history=memory.load_memory_variables | (lambda x: x["history"])     )     | prompt     | llm.bind(stop=["\n\n"]) )  Run the chain with a simple question, expecting an answer aligned with the system message provided.  inputs = {"input": "hi im bob"} response = chain.invoke(inputs) response      AIMessage(content="Ahoy there, me hearty! Yer a fine lookin' swashbuckler, I can see that! *adjusts eye patch* What be bringin' ye to these waters? Are ye here to plunder some booty or just to enjoy the sea breeze?", additional_kwargs={}, example=False)  Save the memory context, then read it back to inspect contents  memory.save_context(inputs, {"output": response.content}) memory.load_memory_variables({})      {'history': [HumanMessage(content='hi im bob', additional_kwargs={}, example=False),       AIMessage(content="Ahoy there, me hearty! Yer a fine lookin' swashbuckler, I can see that! *adjusts eye patch* What be bringin' ye to these waters? Are ye here to plunder some booty or just to enjoy the sea breeze?", additional_kwargs={}, example=False)]}  Now as another question that requires use of the memory.  inputs = {"input": "whats my name"} chain.invoke(inputs)      AIMessage(content="Arrrr, ye be askin' about yer name, eh? Well, me matey, I be knowin' ye as Bob, the scurvy dog! *winks* But if ye want me to call ye somethin' else, just let me know, and I", additional_kwargs={}, example=False)


GigaChat | 🦜🔗 Langchain

Main:  On this page #GigaChat This notebook shows how to use LangChain with [GigaChat](https://developers.sber.ru/portal/products/gigachat). To use you need to install gigachat python package.  # !pip install gigachat  To get GigaChat credentials you need to [create account](https://developers.sber.ru/studio/login) and [get access to API](https://developers.sber.ru/docs/ru/gigachat/api/integration)  ##Example[](#example) import os from getpass import getpass  os.environ["GIGACHAT_CREDENTIALS"] = getpass()  from langchain.chat_models import GigaChat  chat = GigaChat(verify_ssl_certs=False)  from langchain.schema import HumanMessage, SystemMessage  messages = [     SystemMessage(         content="You are a helpful AI that shares everything you know. Talk in English."     ),     HumanMessage(content="Tell me a joke"), ]  print(chat(messages).content)      What do you get when you cross a goat and a skunk? A smelly goat!