Skip to content

Instantly share code, notes, and snippets.

@hwchase17
hwchase17 / langchain_chat_gpt.py
Last active December 5, 2023 16:10
LangChain ChatGPT API Wrapper
from langchain.llms.base import LLM
from typing import Optional, List, Mapping, Any
import requests
from langchain.llms.utils import enforce_stop_tokens
class CustomLLM(LLM):
def __init__(self, url: str):
self.url = url
# STEP 1: Load
# Load documents using LangChain's DocumentLoaders
# This is from https://langchain.readthedocs.io/en/latest/modules/document_loaders/examples/csv.html
from langchain.document_loaders.csv_loader import CSVLoader
loader = CSVLoader(file_path='./example_data/mlb_teams_2012.csv')
data = loader.load()
from langchain.llms import Anthropic
from langchain.agents import load_tools, initialize_agent
from langchain.tools import AIPluginTool
PREFIX = """\n\nHuman: Answer the following questions as best you can. You have access to the following tools:"""
SUFFIX = """Begin!
Question: {input}
\n\nAssistant:
Thought:{agent_scratchpad}"""
# CohereRerank wrapper to use as a document compressor
from typing import Any, Dict, Sequence
from pydantic import root_validator
from langchain.retrievers.document_compressors.base import BaseDocumentCompressor
from langchain.schema import Document
from langchain.utils import get_from_dict_or_env
from langchain.document_loaders import YoutubeLoader
from langchain.indexes import VectorstoreIndexCreator
loader = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=fLn-WqliEQU&lc=UgyOc6oNr_4-YLGCL2R4AaABAg", add_video_info=False)
docs = loader.load()
index = VectorstoreIndexCreator()
index = index.from_documents(docs)
from langchain.document_loaders import YoutubeLoader
from langchain.indexes import VectorstoreIndexCreator
urls = [
("https://www.youtube.com/watch?v=fP6vRNkNEt0", "Prompt Injection"),
("https://www.youtube.com/watch?v=qWv2vyOX0tk", "Low Code-No Code"),
("https://www.youtube.com/watch?v=k8GNCCs16F4", "Agents In Production"),
("https://www.youtube.com/watch?v=1gRlCjy18m4", "Agents"),
("https://www.youtube.com/watch?v=fLn-WqliEQU", "Output Parsing"),
("https://www.youtube.com/watch?v=ywT-5yKDtDg", "Document QA"),
("https://www.youtube.com/watch?v=GrCFyyyAxCU", "SQL"),
System: Respond to the human as helpfully and accurately as possible. You have access to the following tools:
click_element: Click on an element with the given CSS selector, args: {{'selector': {{'title': 'Selector', 'description': 'CSS selector for the element to click', 'type': 'string'}}}}
navigate_browser: Navigate a browser to the specified URL, args: {{'url': {{'title': 'Url', 'description': 'url to navigate to', 'type': 'string'}}}}
previous_webpage: Navigate back to the previous page in the browser history, args: {{}}
extract_text: Extract all the text on the current webpage, args: {{}}
extract_hyperlinks: Extract all hyperlinks on the current webpage, args: {{'absolute_urls': {{'title': 'Absolute Urls', 'description': 'Return absolute URLs instead of relative URLs', 'default': False, 'type': 'boolean'}}}}
get_elements: Retrieve elements in the current web page matching the given CSS selector, args: {{'selector': {{'title': 'Selector', 'description': "CSS selector, such as '*', 'div', 'p', 'a', #id,
from langchain.chat_models import ChatOpenAI
from pydantic import BaseModel, Field
from langchain.document_loaders import UnstructuredURLLoader
from langchain.chains.openai_functions import create_extraction_chain_pydantic
class LLMItem(BaseModel):
title: str = Field(description="The simple and concise title of the product")
description: str = Field(description="The description of the product")
def main():
import pandas as pd
from pandasai import PandasAI
# Sample DataFrame
df = pd.DataFrame({
"country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
"gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
"happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
})
from langchain.chat_models import ChatAnthropic
from kor import create_extraction_chain, Object, Text
llm = ChatAnthropic(
model_name="claude-v2",
temperature=0,
max_tokens=2000,
)
schema = Object(