Skip to content

Instantly share code, notes, and snippets.

from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
import requests
from bs4 import BeautifulSoup
from langchain.schema.runnable import RunnablePassthrough, RunnableLambda
from langchain.utilities import DuckDuckGoSearchAPIWrapper
import json
RESULTS_PER_QUESTION = 3
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}"""
from langchain.vectorstores import Pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
import pinecone
# The environment should be the one specified next to the API key
# in your Pinecone console
pinecone.init(
api_key="...", environment="..."
)
index = pinecone.Index("test123")
class AgentState(TypedDict):
code: str
tests: str
errors: Optional[str]
def initial_writer(state):
...
return {"code": ..., "tests": ...}
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.prompts import PromptTemplate
from langchain.chat_models import ChatAnthropic
from langchain.schema.output_parser import StrOutputParser
#### ROUTER
# This is the router - responsible for chosing what to do
chain = PromptTemplate.from_template("""Given the user question below, classify it as either being about `weather` or `other`.
Do not respond with more than one word.
from langchain.chains.openai_functions import create_structured_output_runnable
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.pydantic_v1 import BaseModel, Field
class Insight(BaseModel):
insight: str = Field(description="""insight""")
chat_model = ChatOpenAI(model_name="gpt-4-1106-preview")
# 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()
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,
@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