File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/python3
2+
3+ import os
4+ from langchain_openai import ChatOpenAI
5+ from langchain_core .runnables import RunnableLambda
6+ from langchain_core .output_parsers import StrOutputParser
7+
8+ def token_counter (text ):
9+ tokens = str (text ).split ()
10+ count = len (tokens )
11+ print (f"Token count: { count } " )
12+ return {"output" : text , "token_count" : count }
13+
14+ model = ChatOpenAI (
15+ base_url = os .getenv ("OPENAI_EP" , "http://localhost:8321/v1/" ),
16+ api_key = "none" ,
17+ model = os .getenv ("INFERENCE_MODEL" , "gemini/models/gemini-flash-latest" )
18+ )
19+
20+ chain = model | StrOutputParser () | RunnableLambda (token_counter )
21+
22+ result = chain .invoke ("Explain the concept of AI agents in one sentence." )
23+ print (f"Final result:\n { result ['output' ]} " )
You can’t perform that action at this time.
0 commit comments