Created
June 13, 2024 07:27
-
-
Save cassc/ec9de77015910b5d172e230feeb5da01 to your computer and use it in GitHub Desktop.
Langchain ChatGPT no memory demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
from pydantic import BaseModel, Field | |
from langchain_core.globals import set_debug | |
from langchain_core.output_parsers.json import JsonOutputParser | |
from langchain_core.prompts.prompt import PromptTemplate | |
from langchain_openai import ChatOpenAI | |
from langchain_community.callbacks import get_openai_callback | |
from langchain.chains import ConversationChain | |
from langchain_community.llms import OpenAI | |
set_debug(False) | |
model_name = 'gpt-4o' | |
class ListResponseModel(BaseModel): | |
data: List[str] = Field(title="List of names, excluding any item already mentioned.") | |
parser = JsonOutputParser(pydantic_object=ListResponseModel) | |
prompt = PromptTemplate.from_template( | |
'In JSON format, give me a list of {topic}.' | |
'{format_instructions}', | |
).partial(format_instructions=parser.get_format_instructions()) | |
model = ChatOpenAI(model=model_name, temperature=0.03) | |
chain = prompt | model | parser | |
for _ in range(3): | |
with get_openai_callback() as cb: | |
# WARN ChatGPT has no memory between this and previous converstations | |
output = chain.invoke(dict(topic='animals')) | |
print(type(output)) | |
print(output) | |
print(f"Total Tokens: {cb.total_tokens}") | |
print(f"Total Cost (USD): ${cb.total_cost}") | |
ListResponseModel.parse_obj(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment