Files
langchain/docs/extras/guides/evaluation/string/Untitled.ipynb
T
William FH 9eb7e6e27f Delete Old Evals Examples (#8252)
Still retain:
- Comparison Examples
- Data + QA walkthrough
- QA (but really minimize it)
2023-07-26 18:46:54 -07:00

9.7 KiB

In [ ]:
import os
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.chat_models import ChatOpenAI
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.schema import (
    SystemMessage,
    HumanMessage,
    AIMessage
)

# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
# os.environ["LANGCHAIN_API_KEY"] = "******"
# os.environ["LANGCHAIN_PROJECT"] = "Jarvis"


prefix_messages = [{"role": "system", "content": "You are a helpful discord Chatbot."}]

llm = ChatOpenAI(model_name='gpt-3.5-turbo', 
             temperature=0.5, 
             max_tokens = 2000)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools,
                         llm,
                         agent="zero-shot-react-description",
                         verbose=True,
                         handle_parsing_errors=True
                         )


async def on_ready():
    print(f'{bot.user} has connected to Discord!')

async def on_message(message):

    print("Detected bot name in message:", message.content)

    # Capture the output of agent.run() in the response variable
    response = agent.run(message.content)

    while response:
        print(response)
        chunk, response = response[:2000], response[2000:]
        print(f"Chunk: {chunk}")
    print("Response sent.")

In [22]:
from dataclasses import dataclass
@dataclass
class Message:
    content: str
In [23]:
await on_message(Message(content="Hi AI, how are you today?"))
Detected bot name in message: Hi AI, how are you today?


> Entering new AgentExecutor chain...
This question is not something that can be answered using the available tools.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:I need to follow the correct format for answering questions.
Action: N/A
Observation: Invalid Format: Missing 'Action Input:' after 'Action:'
Thought:

> Finished chain.
Agent stopped due to iteration limit or time limit.
Chunk: Agent stopped due to iteration limit or time limit.
Response sent.
In [24]:
from langsmith import Client

client = Client()
In [25]:
from itertools import islice

runs = list(islice(client.list_runs(), 10))
In [38]:
runs = sorted(runs, key=lambda x: x.start_time, reverse=True)
In [26]:
ids = [run.id for run in runs]
In [39]:
runs2 = list(client.list_runs(id=ids))
runs2 = sorted(runs2, key=lambda x: x.start_time, reverse=True)
In [42]:
[str(x) for x in ids[:2]]
Out [42]:
['a36092d2-4ad5-4fb4-9b0d-0dba9a2ed836',
 '9398e6be-964f-4aa4-8de9-ad78cd4b7074']
In [48]:
from langsmith.run_helpers import traceable

@traceable(run_type="llm", name="""<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ?start=5" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>""")
def foo():
    return "bar"
In [49]:
foo()
Out [49]:
'bar'
In [52]:
from langchain.llms import AzureOpenAI
In [ ]: