mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-09 19:35:20 +03:00
6.6 KiB
6.6 KiB
In [ ]:
import langchain
from langchain.llms import NIBittensorLLM
import json
from pprint import pprint
langchain.debug = True
# System parameter in NIBittensorLLM is optional but you can set whatever you want to perform with model
llm_sys = NIBittensorLLM(
system_prompt="Your task is to determine response based on user prompt.Explain me like I am technical lead of a project"
)
sys_resp = llm_sys(
"What is bittensor and What are the potential benifits of decentralized AI?"
)
print(f"Response provided by LLM with system prompt set is : {sys_resp}")
# The top_responses parameter can give multiple responses based on its parameter value
# This below code retrive top 10 miner's response all the response are in format of json
# Json response structure is
""" {
"choices": [
{"index": Bittensor's Metagraph index number,
"uid": Unique Identifier of a miner,
"responder_hotkey": Hotkey of a miner,
"message":{"role":"assistant","content": Contains actual response},
"response_ms": Time in millisecond required to fetch response from a miner}
]
} """
multi_response_llm = NIBittensorLLM(top_responses=10)
multi_resp = multi_response_llm("What is Neural Network Feeding Mechanism?")
json_multi_resp = json.loads(multi_resp)
pprint(json_multi_resp)In [ ]:
import langchain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import NIBittensorLLM
langchain.debug = True
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
# System parameter in NIBittensorLLM is optional but you can set whatever you want to perform with model
llm = NIBittensorLLM(system_prompt="Your task is to determine response based on user prompt.")
llm_chain = LLMChain(prompt=prompt, llm=llm)
question = "What is bittensor?"
llm_chain.run(question)In [ ]:
from langchain.agents import (
AgentType,
initialize_agent,
load_tools,
ZeroShotAgent,
Tool,
AgentExecutor,
)
from langchain.memory import ConversationBufferMemory
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.utilities import GoogleSearchAPIWrapper, SerpAPIWrapper
from langchain.llms import NIBittensorLLM
memory = ConversationBufferMemory(memory_key="chat_history")
prefix = """Answer prompt based on LLM if there is need to search something then use internet and observe internet result and give accurate reply of user questions also try to use authenticated sources"""
suffix = """Begin!
{chat_history}
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "chat_history", "agent_scratchpad"],
)
llm = NIBittensorLLM(system_prompt="Your task is to determine response based on user prompt")
llm_chain = LLMChain(llm=llm, prompt=prompt)
memory = ConversationBufferMemory(memory_key="chat_history")
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(
agent=agent, tools=tools, verbose=True, memory=memory
)
response = agent_chain.run(input=prompt)