Skip to content

Instantly share code, notes, and snippets.

@DavesDevFails
Forked from hwchase17/langchain_chat_gpt.py
Created February 25, 2023 00:16
Show Gist options
  • Save DavesDevFails/a9bd6908cf6fbd0e2c529f208269348e to your computer and use it in GitHub Desktop.
Save DavesDevFails/a9bd6908cf6fbd0e2c529f208269348e to your computer and use it in GitHub Desktop.
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
def __call__(self, prompt: str, stop: Optional[List[str]] = None) -> str:
res = requests.get(self.url, params={"q": prompt})
text = res.content.decode()
if stop is not None:
text = enforce_stop_tokens(text, stop)
return text
llm = CustomLLM("http://127.0.0.1:5001/chat")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment