Skip to content

Commit e98e2d6

Browse files
authored
Merge pull request #46 from UiPath/feat/cli-new
feat: add new cli command
2 parents 51696d9 + 0f1e61a commit e98e2d6

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "uipath-langchain"
3-
version = "0.0.94"
3+
version = "0.0.95"
44
description = "UiPath Langchain"
55
readme = { file = "README.md", content-type = "text/markdown" }
66
requires-python = ">=3.10"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": ["."],
3+
"graphs": {
4+
"agent": "./main.py:graph"
5+
},
6+
"env": ".env"
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from langchain_anthropic import ChatAnthropic
2+
from langchain_core.messages import HumanMessage, SystemMessage
3+
from langchain_openai import ChatOpenAI
4+
from langgraph.graph import START, StateGraph, END
5+
from pydantic import BaseModel
6+
import os
7+
8+
class GraphInput(BaseModel):
9+
topic: str
10+
11+
class GraphOutput(BaseModel):
12+
report: str
13+
14+
async def generate_report(state: GraphInput) -> GraphOutput:
15+
if os.getenv("ANTHROPIC_API_KEY"):
16+
llm_model = ChatAnthropic(model="claude-3-5-sonnet-latest")
17+
elif os.getenv("OPENAI_API_KEY"):
18+
llm_model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
19+
else:
20+
raise Exception("Error: Missing API Key. Please define either ANTHROPIC_API_KEY or OPENAI_API_KEY.")
21+
22+
system_prompt = "You are a report generator. Please provide a brief report based on the given topic."
23+
output = await llm_model.ainvoke([SystemMessage(system_prompt), HumanMessage(state.topic)])
24+
return GraphOutput(report=output.content)
25+
26+
builder = StateGraph(input=GraphInput, output=GraphOutput)
27+
28+
builder.add_node("generate_report", generate_report)
29+
30+
builder.add_edge(START, "generate_report")
31+
builder.add_edge("generate_report", END)
32+
33+
graph = builder.compile()

src/uipath_langchain/_cli/cli_new.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
import shutil
3+
4+
import click
5+
from uipath._cli.middlewares import MiddlewareResult
6+
from uipath._cli.spinner import Spinner
7+
8+
9+
def generate_script(target_directory):
10+
template_script_path = os.path.join(
11+
os.path.dirname(__file__), "_templates/main.py.template"
12+
)
13+
target_path = os.path.join(target_directory, "main.py")
14+
15+
shutil.copyfile(template_script_path, target_path)
16+
17+
template_langgraph_json_path = os.path.join(
18+
os.path.dirname(__file__), "_templates/langgraph.json.template"
19+
)
20+
target_path = os.path.join(target_directory, "langgraph.json")
21+
shutil.copyfile(template_langgraph_json_path, target_path)
22+
23+
24+
def generate_pyproject(target_directory, project_name):
25+
project_toml_path = os.path.join(target_directory, "pyproject.toml")
26+
toml_content = f"""[project]
27+
name = "{project_name}"
28+
version = "0.0.1"
29+
description = "{project_name}"
30+
authors = [{{ name = "John Doe", email = "[email protected]" }}]
31+
dependencies = [
32+
"uipath-langchain>=0.0.95",
33+
"langchain-anthropic>=0.3.8",
34+
]
35+
requires-python = ">=3.10"
36+
"""
37+
38+
with open(project_toml_path, "w") as f:
39+
f.write(toml_content)
40+
41+
42+
def langgraph_new_middleware(name: str) -> MiddlewareResult:
43+
"""Middleware to create demo langchain agent"""
44+
spinner = Spinner("Creating demo agent...")
45+
directory = os.getcwd()
46+
47+
try:
48+
generate_script(directory)
49+
click.echo(click.style("✓ ", fg="green", bold=True) + "Created main.py file")
50+
click.echo(
51+
click.style("✓ ", fg="green", bold=True) + "Created langgraph.json file"
52+
)
53+
generate_pyproject(directory, name)
54+
click.echo(
55+
click.style("✓ ", fg="green", bold=True) + "Created pyproject.toml file"
56+
)
57+
os.system("uv sync")
58+
spinner.start()
59+
ctx = click.get_current_context()
60+
init_cmd = ctx.parent.command.get_command(ctx, "init") # type: ignore
61+
ctx.invoke(init_cmd)
62+
spinner.stop()
63+
click.echo(
64+
click.style("✓ ", fg="green", bold=True) + " Agent created successfully."
65+
)
66+
return MiddlewareResult(
67+
should_continue=False,
68+
info_message="""Usage example: ` uipath run agent '{"topic": "UiPath"}' `""",
69+
)
70+
except Exception as e:
71+
spinner.stop()
72+
return MiddlewareResult(
73+
should_continue=False,
74+
error_message=f"❌ Error creating demo agent {str(e)}",
75+
should_include_stacktrace=True,
76+
)

src/uipath_langchain/middlewares.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from uipath._cli.middlewares import Middlewares
22

33
from ._cli.cli_init import langgraph_init_middleware
4+
from ._cli.cli_new import langgraph_new_middleware
45
from ._cli.cli_run import langgraph_run_middleware
56

67

78
def register_middleware():
89
"""This function will be called by the entry point system when uipath_langchain is installed"""
910
Middlewares.register("init", langgraph_init_middleware)
1011
Middlewares.register("run", langgraph_run_middleware)
12+
Middlewares.register("new", langgraph_new_middleware)

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)