So I am experimenting with a conversation chain using LangChain to interface openai and checking the costs. I don’t understand how the first api call calculates a ‘prompt token’ number that is significantly higher than what I expect. Here is the code:
import langchain
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain.callbacks import get_openai_callback
import tiktoken
llm = ChatOpenAI(model_name='gpt-3.5-turbo',
openai_api_key="MY KEY")
memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory = memory,
verbose=True
)
with get_openai_callback() as cb:
input="Hi, my name is Andrew"
tokenizer=tiktoken.get_encoding("cl100k_base")
toks=len(tokenizer.encode(text=input)) # line A
print(toks)
costs={"tokens used":0, "prompt tokens":0, "completion tokens":0, "successful requests":0, "cost (usd)":0}
result = conversation.predict(input=input)
costs['tokens used']=cb.total_tokens
costs['prompt tokens'] = cb.prompt_tokens
costs['completion tokens'] = cb.completion_tokens
costs['successful requests'] = cb.successful_requests
costs['cost (used)'] = cb.total_cost
print(result)
print(costs) #line B
- On line A, I get 6, this is consistent with the openai tokeniser OpenAI Platform
- But then on line A, the output says:
{'tokens used': 87, 'prompt tokens': 70, 'completion tokens': 17, 'successful requests': 1, 'cost (usd)': 0, 'cost (used)': 0.00013900000000000002}
Which suggests the prompt is 70 tokens but in fact I am expecting 6. Am I wrong? What am I missing here?
Thanks!