• <xmp id="om0om">
  • <table id="om0om"><noscript id="om0om"></noscript></table>
  • Generative AI

    NVIDIA NIM?? ??? ?? ?? ?? AI ???? ????

    Reading Time: 7 minutes

    ?? ?? ??(LLM)? ???? AI ????? ??? ???? ???? ????? ???? ??? ? ? ??? ???. ? ?????? ??? ?? ?? ??? ?? ??? ???? ???? ????, ??? ??? ??? ??? ?????. ?? ??? ?? LLM ?? ????? ??? ????? ???? ??? ? ?? ??? ??? ???? ? ????. ??, ?? ???????? ??? ? ?? ???? ??? ????? ?? AI ????? ??? ? ??? ??? ??? ???? ?? ?????.

    ? ?????? NVIDIA? AI ??? ???? ???? API? NVIDIA NIM ???????? ??? ?? ? ? ??(Human-in-the-Loop) AI ????? ???? ???? ??? ?????. ?? ??? ?? ??? ?? ? ??? AI ????? ??? ??? ??? ???? ??? ?????. NIM ???????? ???? ?? LLM? ?????? ??? ??? ? ?? AI ?? ??? ??? ???? ???? ?????. ? ????? ???? ??? ???? ??? ?????? ????? ??? ?? ????? ???? ? ? ??? ? ????.

    ?? ??: NVIDIA NIM?? 5? ?? ??? AI ???? ????

    ???? ?? ??? ???? ?? AI ???? ??

    ??? ?????? ?? ? ?? ? ??? ??? ????? ???? ???? ???? ???? ???? ????. ??? ??? ?? ???? ??? ? ?? ?? ???? ???? ???? ????.

    ????? ???? ??? ??? ??? ??? ??? ??? ???? ?? ????? ????. ??? AI ????? ??? ? ??? ?? ? ????? ??? ? ??? ?????

    ? ?? ????? ??? ????? ????? ??? ???? ?????? ? ??? AI ????? ?? ?????. ?? ????? ?? ???? ??? ?, ?? ??? ?? ?? ???????? ???? ?????. ?? ?? ?????? ?????? ??? ??? ???? ??? ?? ??? ??? ? ????.

    ??? 1. ???? AI ????? ??? ?? ?? ??? ???? ???? ??

    ??-???? ?? ?? ????? ????

    ??? ?? ? ? ?? ???? ????? AI ????? ?? ??? ???? ?? ?? ??? ??? ???? ?? ?????? ???? ???. ?? 1? ?? ?? ????? ???? ?? ?? ??? ???? ?????.

    ?? 1. ?? ???? ?? ?? ??? ????

    ??? ????? ????? NVIDIA LLM NIM ???????? ???? Llama 3.1 405B ??? ?????. ????? ???? ??? ???? ?? NIM ?? ??? ???? ??? ???? LangChain ChatNVIDIA? ???? ????. ChatNVIDIA? NVIDIA? LangChain ????? ??? ?? ?? Python ??????, ???? NVIDIA NIM? ??? ??? ? ??? ???????. ??? ??? LangChain ??? ??(LCEL) ????? ???? ??? ???? ?????? ?????.

    ??? ????? ???? ????

    ??, ??? ????? ????? ?????. ? ????? ?? ?? ?????? ?? NVIDIA API ???? ?? ?? API ?????? ??? ???? ???? ?????. ??, NVIDIA AI Enterprise ??? NIM ?????? ?? ??? ????? ??? ?? ????.

    ????? ?? Python ??? ?????:

    from langchain_nvidia_ai_endpoints import ChatNVIDIA
    from langchain import prompts, chat_models, hub
    from langchain_core.output_parsers import StrOutputParser
    from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
    from langchain_core.pydantic_v1 import BaseModel, Field, validator
    from typing import Optional, List
     
     
    ## 1. construct the system prompt ---------
    prompt_template = """
    ### [INST]
     
     
    You are an expert social media content creator.
    Your task is to create a different promotion message with the following 
    Product Description :
    ------
    {product_desc}
    ------
    The output promotion message MUST use the following format :
    '''
    Title: a powerful, short message that dipict what this product is about 
    Message: be creative for the promotion message, but make it short and ready for social media feeds.
    Tags: the hash tag human will nomally use in social media
    '''
    Begin!
    [/INST]
     """
    prompt = PromptTemplate(
    input_variables=['produce_desc'],
    template=prompt_template,
    )
     
     
    ## 2. provide seeded product_desc text
    product_desc="Explore the latest community-built AI models with an API optimized and accelerated by NVIDIA, then deploy anywhere with NVIDIA NIM? inference microservices."
     
     
    ## 3. structural output using LMFE 
    class StructureOutput(BaseModel):     
        Title: str = Field(description="Title of the promotion message")
        Message : str = Field(description="The actual promotion message")
        Tags: List[str] = Field(description="Hashtags for social media, usually starts with #")
     
     
    ## 4. A powerful LLM 
    llm_with_output_structure=ChatNVIDIA(model="meta/llama-3.1-405b-instruct").with_structured_output(StructureOutput)     
     
     
    ## construct the content_creator agent
    content_creator = ( prompt | llm_with_output_structure )
    out=content_creator.invoke({"product_desc":product_desc})

    ??? ???? ???? ??

    ???? NVIDIA sdXL-turbo ???-??? ?? ??? ???? ?? ???? ???? ???? ???? ??? ???? ????? ?????. ? ????? ?? ??? ?? ???? ?? ??? ???? ???? ?? ???? ??? ???? ?????. ?? ??? ????? ??? ????? ???? ?????:

    import requests
    import base64, io
    from PIL import Image
    import requests, json
    def generate_image(prompt :str) -> str :
        """
        generate image from text
        Args:
            prompt: input text
        """
        ## re-writing the input promotion title in to appropriate image_gen prompt 
        gen_prompt=llm_rewrite_to_image_prompts(prompt)
        print("start generating image with llm re-write prompt:", gen_prompt)
        invoke_url = "https://ai.api.nvidia.com/v1/genai/stabilityai/sdxl-turbo"
         
        headers = {
            "Authorization": f"Bearer {nvapi_key}",
            "Accept": "application/json",
        }
         
        payload = {
            "text_prompts": [{"text": gen_prompt}],
            "seed": 0,
            "sampler": "K_EULER_ANCESTRAL",
            "steps": 2
        }
         
        response = requests.post(invoke_url, headers=headers, json=payload)
         
        response.raise_for_status()
        response_body = response.json()
        ## load back to numpy array 
        print(response_body['artifacts'][0].keys())
        imgdata = base64.b64decode(response_body["artifacts"][0]["base64"])
        filename = 'output.jpg'
        with open(filename, 'wb') as f:
            f.write(imgdata)   
        im = Image.open(filename)  
        img_location=f"the output of the generated image will be stored in this path : {filename}"
        return img_location

    ?? Python ????? ???? ??? ?? ??? ??? ?? ????? ??????:

    from langchain_nvidia_ai_endpoints import ChatNVIDIA
    from langchain import prompts, chat_models, hub
    from langchain_core.output_parsers import StrOutputParser
    from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
     
     
    def llm_rewrite_to_image_prompts(user_query):
        prompt = prompts.ChatPromptTemplate.from_messages(
            [
                (
                    "system",
                    "Summarize the following user query into a very short, one-sentence theme for image generation, MUST follow this format : A iconic, futuristic image of , no text, no amputation, no face, bright, vibrant",
                ),
                ("user", "{input}"),
            ]
        )
        model = ChatNVIDIA(model="mistralai/mixtral-8x7b-instruct-v0.1")
        chain = ( prompt    | model   | StrOutputParser() )
        out= chain.invoke({"input":user_query})
        #print(type(out))
        return out}

    ?? ?? ??? ??? ??? LLM? ????? LCEL? ???? ??? ???? ????? ?????:

    ## bind image generation as tool into llama3.1-405b llm
    llm=ChatNVIDIA(model="meta/llama-3.1-405b-instruct")
    llm_with_img_gen_tool=llm.bind_tools([generate_image],tool_choice="generate_image")
    ## use LCEL to construct Digital Artist Agent
    digital_artist = (
        llm_with_img_gen_tool
        | output_to_invoke_tools
    )

    ?? ? ? ??? ?? ????? ?? ??

    ??? ??? ???? ??, ????? ?? ??? ?? ?? ???? ?????. ??????? ??? ????? ????? ??? ???? ??? ???? ????? ??? ????? ?? ?????.

    ??? ????? ?? ??? ???, ?? ?? ?? ???? ???? ??? ???? ?? ??? ?????? ??? ? ????.

    ???? ??? ??? ?? ???? ??? ?? ? ??? ??? ????? ?????. LangGraph? ???? ?? ????? ???? ? ?????.

    ???? ??? ??? ???? ??? ?????:

    # Or you can directly instantiate the tool
    from langchain_community.tools import HumanInputRun
    from langchain.agents import AgentType, load_tools
    from langchain.agents import AgentType, initialize_agent, load_tools
     
     
    def get_human_input() -> str:
        """ Put human as decision maker, human will decide which agent is best for the task"""
        print("You have been given 2 agents. Please select exactly _ONE_ agent to help you with the task, enter 'y' to confirm your choice.")
        print("""Available agents are : \n
                1 ContentCreator  \n
                2 DigitalArtist \n          
                Enter 1 or 2""")
        contents = []
        while True:
            try:            
                line = input()
                if line=='1':
                    tool="ContentCreator"               
                    line=tool                
                elif line=='2':
                    tool="DigitalArtist"               
                    line=tool                
                else:
                    pass
                 
            except EOFError:
                break
            if line == "y":
                print(f"tool selected : {tool} ")
                break
            contents.append(line)       
        return "\n".join(contents)
     
     
    # You can modify the tool when loading
     
     
    ask_human = HumanInputRun(input_func=get_human_input)

    ????, ??? ?? ??? ? ? ?? Python ??? ??? ???? LangGraph? ????? ?? ?? ?? ??? ???? ? ?????. ??? ??? ???? ????? ?? ??? ????? ?? ??? ???? ???? ???? ????? ?? ? ????:

    from langgraph.graph import END, StateGraph
    from langgraph.prebuilt import ToolInvocation
    from colorama  import Fore,Style
    # Define the functions needed 
    def human_assign_to_agent(state):
        # ensure using original prompt 
        inputs = state["input"]
        input_to_agent = state["input_to_agent"]
        concatenate_str = Fore.BLUE+inputs+ ' : '+Fore.CYAN+input_to_agent + Fore.RESET
        print(concatenate_str)
        print("---"*10)  
        agent_choice=ask_human.invoke(concatenate_str)
        print(Fore.CYAN+ "choosen_agent : " + agent_choice + Fore.RESET)
        return {"agent_choice": agent_choice }
     
     
    def agent_execute_task(state):    
        inputs= state["input"]
        input_to_agent = state["input_to_agent"]
        print(Fore.CYAN+input_to_agent + Fore.RESET)
        # choosen agent will execute the task
        choosen_agent = state['agent_choice']
        if choosen_agent=='ContentCreator':
            structured_respond=content_creator.invoke({"product_desc":input_to_agent})
            respond='\n'.join([structured_respond.Title,structured_respond.Message,''.join(structured_respond.Tags)])       
        elif choosen_agent=="DigitalArtist":
            respond=digital_artist.invoke(input_to_agent)
        else:
            respond="please reselect the agent, there are only 2 agents available: 1.ContentCreator or 2.DigitalArtist"
         
        print(Fore.CYAN+ "agent_output: \n" + respond + Fore.RESET)
        return {"agent_use_tool_respond": respond} 

    ????? ??? ??? ???? ?? ?? ???? ?? ? ? ?? ?? ???? ?????? ?????. ?? ??? ??? ?? ???? ??????.:

    from langgraph.graph import END, StateGraph
     
     
    # Define a new graph
    workflow = StateGraph(State)
     
     
    # Define the two nodes 
    workflow.add_node("start", human_assign_to_agent)
    workflow.add_node("end", agent_execute_task)
     
     
    # This means that this node is the first one called
    workflow.set_entry_point("start")
    workflow.add_edge("start", "end")
    workflow.add_edge("end", END)
     
     
    # Finally, we compile it!
    # This compiles it into a LangChain Runnable,
    # meaning you can use it as you would any other runnable
    app = workflow.compile()

    ?? ???? ????? ????

    ?? ?? ?????. ??? ??? ?? ??? ???? ? ? ?? ????? ???? ?????.

    ???? ??? ??? ?? ????

    ?? ??? ????? ?????? ??, ??? ? ?? ??? ????? ??? ???? ???? ????? ?????(?? 2). ??? ????? ??? ? ??? ?????.

    ?? 2. ??? ??? ????? ????? ?? ??? ???? ???? ???? ?? ???? ??

    Python ?? ?????:

    my_query="create a good promotional message for social promotion events using the following inputs"
    product_desc="NVIDIA NIM microservices power GenAI workflow"
    respond=app.invoke({"input":my_query, "input_to_agent":product_desc})

    ??? ??? ?? 1 = ??? ??? ????? ?????. ????? ?? 3? ?? agent_output ? ???? ?????.

    ?? 3. ??? ????? ???? ????? ??? ?? ??

    ????? ??? ?? ????

    ??? ?????? ??? ???? ?????? ?? ??? ??? ???? ???? ?????(?? 4).

    ?? Python ?? ??? ??? ????? ????? ??? ??? ??? ????? ???? ?????:

    ## taken the output from the Title from the output of Content Creator Agent 
    prompt_for_image=respond['agent_use_tool_respond'].split('\n')[0].split(':')[-1].strip()
    ## Human decision maker give instruction to the agent workflow app
    input_query="generate an image for me from the below promotion message"
    respond2=app.invoke({"input":input_query, "input_to_agent":prompt_for_image})

    ??? ???? output.jpg? ?????.

    ?? 5. ??? ???? ???? ????? ??? ?? ??

    ??? ???? ?? ?? ??

    ??? ???? ???? ??? ??? ?? ?? ??? ???? ??? ?? ? ????(?? 6). ??? ????? ????? ?? ????? ?? ???? ??? ???? ?????? ??? ???? ?? ? ????.

    ?? 6. ??? ???? ????? ??? ?? ???

    ?? ??? ???

    ????? ???? ???? ? ????? ??? ???? ??? ?? ??? ??? ?? ???? ???? ??? ?????(?? 7).

    ?? 7. AI ????? ???? ??? ??? ?? ??? ??? ? ?? ???? ??? ???

    NVIDIA NIM ??????? ? AI ?? AI ???? ????

    ? ??? ?????? ??? ?? ?????? ????? ?? NVIDIA NIM ???????? LangChain? LangGraph? ???? ?? ? ? ?? AI ????? ???? ??? ?? ???????. ?????? AI ????? ???? ??? ?? ??? ???, ???? ???, ?????? ????? ???? ??? ? ????.

    NVIDIA NIM ???????? ???? AI ?? ??? ????? ???? ??? ? ????. ?? ???? ???? ???? ?????, ?? ? ? ?? AI ????? ?????? ????? ???? ?? ? ?? ??? ???? ?????.

    ?? ???? ?? ??? ?????:

    ?? ???

    Discuss (0)
    +1

    Tags

    人人超碰97caoporen国产