Spaces:
Runtime error
Runtime error
Download app.py from canon27/Essay-Writer-AI: direct link, hf CLI and curl.
- Browser
- Download file 5.26 kB
-
https://huggingface.co/spaces/canon27/Essay-Writer-AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/canon27/Essay-Writer-AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/canon27/Essay-Writer-AI/resolve/main/app.py
5.26 kB
| import os | |
| os.system("pip install langgraph") | |
| from langgraph.checkpoint.sqlite import SqliteSaver | |
| from lamini import LaminiClient | |
| from langgraph.graph import StateGraph, END | |
| from typing import TypedDict, Annotated, List | |
| import operator | |
| # from langgraph.checkpoint import SqliteSaver | |
| from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage, AIMessage, ChatMessage | |
| from duckduckgo_search import DDGS | |
| memory = SqliteSaver.from_conn_string(":memory:") | |
| class AgentState(TypedDict): | |
| task: str | |
| plan: str | |
| draft: str | |
| critique: str | |
| content: List[str] | |
| revision_number: int | |
| max_revisions: int | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| #model_name = "mistralai/Mistral-7B-Instruct" | |
| #tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| #model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto") | |
| lamini = LaminiClient(api_key=os.environ["LAMINI_API_KEY"]) | |
| def chat(input_text): | |
| inputs = tokenizer(input_text, return_tensors="pt").to("cuda") | |
| output = model.generate(**inputs, max_new_tokens=300) | |
| return tokenizer.decode(output[0], skip_special_tokens=True) | |
| def search_online(query): | |
| """使用 DuckDuckGo 进行免费搜索""" | |
| with DDGS() as ddgs: | |
| results = ddgs.text(query, max_results=5) | |
| return [r["title"] + " - " + r["href"] for r in results] | |
| PLAN_PROMPT = """You are an expert writer tasked with writing a high level outline of an essay. \ | |
| Write such an outline for the user provided topic. Give an outline of the essay along with any relevant notes \ | |
| or instructions for the sections.""" | |
| WRITER_PROMPT = """You are an essay assistant tasked with writing excellent 5-paragraph essays.\ | |
| Generate the best essay possible for the given topic while maintaining coherence and logical flow.""" | |
| REFLECTION_PROMPT = """You are a critical reviewer tasked with evaluating an essay draft.\ | |
| Provide constructive feedback on clarity, structure, and argumentation.""" | |
| RESEARCH_PLAN_PROMPT = """You are a research strategist tasked with creating a research plan.\ | |
| Generate a plan detailing the key sources, arguments, and methodologies relevant to the topic.""" | |
| RESEARCH_CRITIQUE_PROMPT = """You are an academic reviewer tasked with critiquing research plans.\ | |
| Evaluate the validity, coherence, and feasibility of the proposed research approach.""" | |
| def plan_node(state: AgentState): | |
| messages = [ | |
| SystemMessage(content=PLAN_PROMPT), | |
| HumanMessage(content=state['task']) | |
| ] | |
| response = model.invoke(messages) | |
| return {"plan": response.content} | |
| def research_plan_node(state: AgentState): | |
| queries = model.with_structured_output(Queries).invoke([ | |
| SystemMessage(content=RESEARCH_PLAN_PROMPT), | |
| HumanMessage(content=state['task']) | |
| ]) | |
| content = state['content'] or [] | |
| for q in queries.queries: | |
| response = search_online(q) | |
| content.extend(response) | |
| return {"content": content} | |
| def generation_node(state: AgentState): | |
| content = "\n\n".join(state['content'] or []) | |
| user_message = HumanMessage( | |
| content=f"{state['task']}\n\nHere is my plan:\n\n{state['plan']}") | |
| messages = [ | |
| SystemMessage(content=WRITER_PROMPT.format(content=content)), | |
| user_message | |
| ] | |
| response = model.invoke(messages) | |
| return { | |
| "draft": response.content, | |
| "revision_number": state.get("revision_number", 1) + 1 | |
| } | |
| def reflection_node(state: AgentState): | |
| messages = [ | |
| SystemMessage(content=REFLECTION_PROMPT), | |
| HumanMessage(content=state['draft']) | |
| ] | |
| response = model.invoke(messages) | |
| return {"critique": response.content} | |
| def research_critique_node(state: AgentState): | |
| queries = model.with_structured_output(Queries).invoke([ | |
| SystemMessage(content=RESEARCH_CRITIQUE_PROMPT), | |
| HumanMessage(content=state['critique']) | |
| ]) | |
| content = state['content'] or [] | |
| for q in queries.queries: | |
| response = search_online(q) | |
| content.extend(response) | |
| return {"content": content} | |
| def should_continue(state): | |
| if state["revision_number"] > state["max_revisions"]: | |
| return END | |
| return "reflect" | |
| builder = StateGraph(AgentState) | |
| builder.add_node("planner", plan_node) | |
| builder.add_node("generate", generation_node) | |
| builder.add_node("reflect", reflection_node) | |
| builder.add_node("research_plan", research_plan_node) | |
| builder.add_node("research_critique", research_critique_node) | |
| builder.set_entry_point("planner") | |
| builder.add_conditional_edges( | |
| "generate", | |
| should_continue, | |
| {END: END, "reflect": "reflect"} | |
| ) | |
| builder.add_edge("planner", "research_plan") | |
| builder.add_edge("research_plan", "generate") | |
| builder.add_edge("reflect", "research_critique") | |
| builder.add_edge("research_critique", "generate") | |
| graph = builder.compile(checkpointer=memory) | |
| thread = {"configurable": {"thread_id": "1"}} | |
| for s in graph.stream({ | |
| 'task': "what is the difference between langchain and langsmith", | |
| "max_revisions": 2, | |
| "revision_number": 1, | |
| }, thread): | |
| print(s) | |
| import warnings | |
| warnings.filterwarnings("ignore") | |
| from helper import ewriter, writer_gui | |
| MultiAgent = ewriter() | |
| app = writer_gui(MultiAgent.graph) | |
| app.launch() | |