-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathlangchainv1_basic.py
More file actions
47 lines (39 loc) · 1.48 KB
/
langchainv1_basic.py
File metadata and controls
47 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import azure.identity
from dotenv import load_dotenv
from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from rich import print
load_dotenv(override=True)
API_HOST = os.getenv("API_HOST", "github")
if API_HOST == "azure":
token_provider = azure.identity.get_bearer_token_provider(
azure.identity.DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default",
)
model = ChatOpenAI(
model=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"),
base_url=os.environ["AZURE_OPENAI_ENDPOINT"] + "/openai/v1",
api_key=token_provider,
)
elif API_HOST == "github":
model = ChatOpenAI(
model=os.getenv("GITHUB_MODEL", "gpt-4o"),
base_url="https://models.inference.ai.azure.com",
api_key=os.environ.get("GITHUB_TOKEN"),
)
elif API_HOST == "ollama":
model = ChatOpenAI(
model=os.environ.get("OLLAMA_MODEL", "llama3.1"),
base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"),
api_key="none",
)
else:
model = ChatOpenAI(model=os.getenv("OPENAI_MODEL", "gpt-4o-mini"))
agent = create_agent(model=model, system_prompt="You're an informational agent. Answer questions cheerfully.", tools=[])
def main():
response = agent.invoke({"messages": [{"role": "user", "content": "Whats weather today in San Francisco?"}]})
latest_message = response["messages"][-1]
print(latest_message.content)
if __name__ == "__main__":
main()