Skip to content

Commit

Permalink
Merge branch 'update_README' of github.com:ZiTao-Li/agentscope into z…
Browse files Browse the repository at this point in the history
…t/update_readme
  • Loading branch information
pan-x-c committed Mar 18, 2024
2 parents 6e13296 + 34e7364 commit eee06fe
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/conversation_with_langchain/READMD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Create an Agent with LangChain

AgentScope is a highly flexible multi-agent platform. It allows developers
to create agents with third-party libraries.

In this example, we will show how to create an assistant agent with
LangChain in AgentScope, and interact with user in a conversation.

**Note** we use OpenAI API for LangChain in this example. Developers can
modify it according to their own needs.

## Install LangChain

Before running the example, please install LangChain by the following command:
```bash
pip install langchain==0.1.11 langchain-openai==0.0.8
```

## Create Agent with LangChain

In this example, the memory management, prompt engineering, and model
invocation are all handled by LangChain.
Specifically, we create an agent class named `LangChainAgent`.
In its `reply` function, developers only need parse the input message and
wrap the output message into `agentscope.message.Msg` class.
After that, developers can build the conversation in AgentScope, and the
`LangChainAgent` is the same as other agents in AgentScope.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
"""A simple example of using langchain to create an assistant agent in
AgentScope."""
import os
from typing import Optional

from langchain_openai import OpenAI
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

import agentscope
from agentscope.agents import AgentBase
from agentscope.agents import UserAgent
from agentscope.message import Msg


class LangChainAgent(AgentBase):
"""An agent that implemented by langchain."""

def __init__(self, name: str) -> None:
"""Initialize the agent."""

# Disable AgentScope memory and use langchain memory instead
super().__init__(name, use_memory=False)

# [START] BY LANGCHAIN
# Create a memory in langchain
memory = ConversationBufferMemory(memory_key="chat_history")

# Prepare prompt
template = """
You are a helpful assistant, and your goal is to help the user.
{chat_history}
Human: {human_input}
Assistant:"""

prompt = PromptTemplate(
input_variables=["chat_history", "human_input"],
template=template,
)

llm = OpenAI(openai_api_key=os.environ["OPENAI_API_KEY"])

# Prepare a chain and manage the memory by LLMChain in langchain
self.llm_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=False,
memory=memory,
)
# [END] BY LANGCHAIN

def reply(self, x: Optional[dict] = None) -> Msg:
# [START] BY LANGCHAIN

# Generate response
response_str = self.llm_chain.predict(human_input=x.content)

# [END] BY LANGCHAIN

# Wrap the response in a message object in AgentScope
return Msg(name=self.name, content=response_str)


# Build a conversation between user and assistant agent

# init AgentScope
agentscope.init()

# Create an instance of the langchain agent
agent = LangChainAgent(name="Assistant")

# Create a user agent from AgentScope
user = UserAgent("User")

msg = None
while True:
# User input
msg = user(msg)
if msg.content == "exit":
break
# Agent speaks
msg = agent(msg)
2 changes: 2 additions & 0 deletions src/agentscope/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .sql_query.mongodb import query_mongodb
from .web_search.search import bing_search, google_search
from .service_response import ServiceResponse
from .service_factory import ServiceFactory
from .retrieval.similarity import cos_sim
from .text_processing.summarization import summarization
from .retrieval.retrieval_from_list import retrieve_from_list
Expand Down Expand Up @@ -48,6 +49,7 @@ def get_help() -> None:
"query_sqlite",
"query_mongodb",
"ServiceResponse",
"ServiceFactory",
"cos_sim",
"summarization",
"retrieve_from_list",
Expand Down

0 comments on commit eee06fe

Please sign in to comment.