run CrewAI python script with node-calls-python package
This commit is contained in:
33
src/app/api/graphql/crew_ai.js
Normal file
33
src/app/api/graphql/crew_ai.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import prisma from "@/utils/prisma";
|
||||
|
||||
const nodecallspython = require("node-calls-python");
|
||||
|
||||
const py = nodecallspython.interpreter;
|
||||
|
||||
py.addImportPath(
|
||||
"E:\\Crew AI\\TheProject\\crew-ai-visualizer\\src\\app\\api\\graphql\\venv\\Lib\\site-packages"
|
||||
);
|
||||
|
||||
export function runMission(id) {
|
||||
const crewaiPath =
|
||||
"E:\\Crew AI\\TheProject\\crew-ai-visualizer\\src\\app\\api\\graphql\\crew_ai.py";
|
||||
return py
|
||||
.import(crewaiPath)
|
||||
.then(async function (pymodule) {
|
||||
const mission = await prisma.mission.findFirst({
|
||||
where: { id },
|
||||
include: { crew: true },
|
||||
});
|
||||
if (mission) {
|
||||
const result = await py.call(pymodule, "run_mission", mission);
|
||||
const missionWithResult = prisma.mission.update({
|
||||
where: { id },
|
||||
data: { result },
|
||||
});
|
||||
return missionWithResult;
|
||||
} else {
|
||||
throw Error("Mission doest not exist");
|
||||
}
|
||||
})
|
||||
.catch((err) => console.log(err));
|
||||
}
|
||||
57
src/app/api/graphql/crew_ai.py
Normal file
57
src/app/api/graphql/crew_ai.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import os
|
||||
|
||||
from textwrap import dedent
|
||||
from crewai import Agent, Task, Crew, Process
|
||||
from langchain_google_genai import ChatGoogleGenerativeAI
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
process_type = {
|
||||
"SEQUENTIAL": Process.sequential,
|
||||
"HIERARTICAL": Process.hierarchical,
|
||||
}
|
||||
|
||||
|
||||
def run_mission(mission):
|
||||
llm = ChatGoogleGenerativeAI(
|
||||
model="gemini-pro",
|
||||
verbose=True,
|
||||
temperature=0.5,
|
||||
google_api_key=os.getenv("GEMINI_API_KEY"),
|
||||
)
|
||||
|
||||
agents = [
|
||||
Agent(
|
||||
role=agent["role"],
|
||||
goal=agent["goal"],
|
||||
backstory=agent["backstory"],
|
||||
allow_delegation=agent["allowDelegation"],
|
||||
verbose=agent["verbose"],
|
||||
llm=llm,
|
||||
)
|
||||
for agent in mission["crew"]
|
||||
]
|
||||
|
||||
tasks = [
|
||||
Task(
|
||||
description=dedent(task["description"]),
|
||||
agent=(
|
||||
[agent for agent in agents if agent.role == task["agent"]["role"]][0]
|
||||
if task["agent"]
|
||||
else None
|
||||
),
|
||||
)
|
||||
for task in mission["tasks"]
|
||||
]
|
||||
|
||||
crew = Crew(
|
||||
agents=agents,
|
||||
tasks=tasks,
|
||||
verbose=mission["verbose"],
|
||||
process=process_type[mission["process"]],
|
||||
manager_llm=llm,
|
||||
)
|
||||
|
||||
result = crew.kickoff()
|
||||
return result
|
||||
77
src/app/api/graphql/requirements.txt
Normal file
77
src/app/api/graphql/requirements.txt
Normal file
@@ -0,0 +1,77 @@
|
||||
aiohttp==3.9.3
|
||||
aiosignal==1.3.1
|
||||
annotated-types==0.6.0
|
||||
anyio==4.3.0
|
||||
attrs==23.2.0
|
||||
backoff==2.2.1
|
||||
cachetools==5.3.2
|
||||
certifi==2024.2.2
|
||||
charset-normalizer==3.3.2
|
||||
click==8.1.7
|
||||
colorama==0.4.6
|
||||
crewai==0.11.2
|
||||
dataclasses-json==0.6.4
|
||||
Deprecated==1.2.14
|
||||
distro==1.9.0
|
||||
docstring-parser==0.15
|
||||
frozenlist==1.4.1
|
||||
google-ai-generativelanguage==0.4.0
|
||||
google-api-core==2.17.1
|
||||
google-auth==2.28.0
|
||||
google-generativeai==0.3.2
|
||||
googleapis-common-protos==1.62.0
|
||||
greenlet==3.0.3
|
||||
grpcio==1.60.1
|
||||
grpcio-status==1.60.1
|
||||
h11==0.14.0
|
||||
httpcore==1.0.3
|
||||
httpx==0.26.0
|
||||
idna==3.6
|
||||
importlib-metadata==6.11.0
|
||||
instructor==0.5.2
|
||||
jsonpatch==1.33
|
||||
jsonpointer==2.4
|
||||
langchain==0.1.8
|
||||
langchain-community==0.0.21
|
||||
langchain-core==0.1.24
|
||||
langchain-google-genai==0.0.9
|
||||
langchain-openai==0.0.5
|
||||
langsmith==0.1.3
|
||||
markdown-it-py==3.0.0
|
||||
marshmallow==3.20.2
|
||||
mdurl==0.1.2
|
||||
multidict==6.0.5
|
||||
mypy-extensions==1.0.0
|
||||
numpy==1.26.4
|
||||
openai==1.12.0
|
||||
opentelemetry-api==1.22.0
|
||||
opentelemetry-exporter-otlp-proto-common==1.22.0
|
||||
opentelemetry-exporter-otlp-proto-http==1.22.0
|
||||
opentelemetry-proto==1.22.0
|
||||
opentelemetry-sdk==1.22.0
|
||||
opentelemetry-semantic-conventions==0.43b0
|
||||
packaging==23.2
|
||||
proto-plus==1.23.0
|
||||
protobuf==4.25.3
|
||||
pyasn1==0.5.1
|
||||
pyasn1-modules==0.3.0
|
||||
pydantic==2.6.1
|
||||
pydantic_core==2.16.2
|
||||
Pygments==2.17.2
|
||||
PyYAML==6.0.1
|
||||
regex==2023.12.25
|
||||
requests==2.31.0
|
||||
rich==13.7.0
|
||||
rsa==4.9
|
||||
sniffio==1.3.0
|
||||
SQLAlchemy==2.0.27
|
||||
tenacity==8.2.3
|
||||
tiktoken==0.5.2
|
||||
tqdm==4.66.2
|
||||
typer==0.9.0
|
||||
typing-inspect==0.9.0
|
||||
typing_extensions==4.9.0
|
||||
urllib3==2.2.1
|
||||
wrapt==1.16.0
|
||||
yarl==1.9.4
|
||||
zipp==3.17.0
|
||||
@@ -1,17 +1,13 @@
|
||||
import { Process } from "@/data/consts";
|
||||
import { Agent } from "@/types/agent";
|
||||
import { CreateMissionInput, Mission } from "@/types/mission";
|
||||
import { Task } from "@/types/task";
|
||||
import prisma from "@/utils/prisma";
|
||||
import { GraphQLResolveInfo } from "graphql";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { runMission } from "./crew_ai";
|
||||
|
||||
const resolvers = {
|
||||
Query: {
|
||||
agents: () => {
|
||||
return prisma.agent.findMany();
|
||||
},
|
||||
agent: (id: number) => {
|
||||
agent: (id) => {
|
||||
return prisma.agent.findFirst({
|
||||
where: {
|
||||
id: id,
|
||||
@@ -26,7 +22,7 @@ const resolvers = {
|
||||
});
|
||||
return missions;
|
||||
},
|
||||
mission: (id: number) => {
|
||||
mission: (id) => {
|
||||
return prisma.mission.findFirst({
|
||||
where: {
|
||||
id: id,
|
||||
@@ -35,42 +31,22 @@ const resolvers = {
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
createAgent: async (
|
||||
parent: any,
|
||||
body: Agent,
|
||||
context: { req: NextRequest; res: NextResponse; datasource: any },
|
||||
info: GraphQLResolveInfo
|
||||
) => {
|
||||
createAgent: async (parent, body, context, info) => {
|
||||
const agent = await prisma.agent.create({ data: body });
|
||||
return agent;
|
||||
},
|
||||
updateAgent: async (
|
||||
parent: any,
|
||||
body: Agent,
|
||||
context: { req: NextRequest; res: NextResponse; datasource: any },
|
||||
info: GraphQLResolveInfo
|
||||
) => {
|
||||
updateAgent: async (parent, body, context, info) => {
|
||||
const updatedAgent = await prisma.agent.update({
|
||||
where: { id: body.id },
|
||||
data: body,
|
||||
});
|
||||
return updatedAgent;
|
||||
},
|
||||
deleteAgent: async (
|
||||
parent: any,
|
||||
body: { id: number },
|
||||
context: { req: NextRequest; res: NextResponse; datasource: any },
|
||||
info: GraphQLResolveInfo
|
||||
) => {
|
||||
deleteAgent: async (parent, body, context, info) => {
|
||||
await prisma.agent.delete({ where: { id: body.id } });
|
||||
return { deleted: true };
|
||||
},
|
||||
createMission: async (
|
||||
parent: any,
|
||||
body: CreateMissionInput,
|
||||
context: { req: NextRequest; res: NextResponse; datasource: any },
|
||||
info: GraphQLResolveInfo
|
||||
) => {
|
||||
createMission: async (parent, body, context, info) => {
|
||||
const { name, verbose, process } = body;
|
||||
const crew = await prisma.agent.findMany({
|
||||
where: {
|
||||
@@ -79,7 +55,7 @@ const resolvers = {
|
||||
},
|
||||
},
|
||||
});
|
||||
const tasks: Array<Task> = [];
|
||||
const tasks = [];
|
||||
for (let task of body.tasks) {
|
||||
const agent = await prisma.agent.findFirst({
|
||||
where: { id: task.agent },
|
||||
@@ -102,12 +78,7 @@ const resolvers = {
|
||||
mission.id;
|
||||
return mission;
|
||||
},
|
||||
updateMission: async (
|
||||
parent: any,
|
||||
body: CreateMissionInput,
|
||||
context: { req: NextRequest; res: NextResponse; datasource: any },
|
||||
info: GraphQLResolveInfo
|
||||
) => {
|
||||
updateMission: async (parent, body, context, info) => {
|
||||
const { id, name, verbose, process } = body;
|
||||
const crew = await prisma.agent.findMany({
|
||||
where: {
|
||||
@@ -116,7 +87,7 @@ const resolvers = {
|
||||
},
|
||||
},
|
||||
});
|
||||
const tasks: Array<Task> = [];
|
||||
const tasks = [];
|
||||
if (body.tasks) {
|
||||
for (let task of body.tasks) {
|
||||
const agent = await prisma.agent.findFirst({
|
||||
@@ -146,21 +117,14 @@ const resolvers = {
|
||||
});
|
||||
return mission;
|
||||
},
|
||||
deleteMission: async (
|
||||
parent: any,
|
||||
body: { id: number },
|
||||
context: { req: NextRequest; res: NextResponse; datasource: any },
|
||||
info: GraphQLResolveInfo
|
||||
) => {
|
||||
deleteMission: async (parent, body, context, info) => {
|
||||
await prisma.mission.delete({ where: { id: body.id } });
|
||||
return { deleted: true };
|
||||
},
|
||||
runMission: async (
|
||||
parent: any,
|
||||
body: { id: number },
|
||||
context: { req: NextRequest; res: NextResponse; datasource: any },
|
||||
info: GraphQLResolveInfo
|
||||
) => {},
|
||||
runMission: async (parent, body, context, info) => {
|
||||
const result = await runMission(body.id);
|
||||
return result;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user