Skip to content

Commit 71b079f

Browse files
committed
langchain_demo.py
1 parent ab8319b commit 71b079f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

llm/langchain_demo.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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']}")

0 commit comments

Comments
 (0)