Skip to content

Instantly share code, notes, and snippets.

# 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.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(
from langchain import PromptTemplate, OpenAI, LLMChain
prompt_template = "What is a good name for a company that makes {product}?"
llm1 = OpenAI(model="foo", temperature=0)
llm_chain1 = LLMChain(
llm=llm1,
prompt=PromptTemplate.from_template(prompt_template)
)
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
llm = OpenAI(temperature=0, model="gpt-3.5-turbo-instruct")
from metaphor_python import Metaphor
client = Metaphor("")
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.schema.output_parser import StrOutputParser
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.runnable import RunnableMap
from langchain.schema import format_document
from typing import AsyncGenerator
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():
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"),
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)
@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
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,