lizhen commited on
Commit
d02575f
·
1 Parent(s): 037db72

完成快速入门。

Browse files
app.py CHANGED
@@ -6,12 +6,13 @@ def chatOpenAI(input):
6
  return llm(input)
7
 
8
  with gr.Blocks() as demo:
 
9
  gr.Markdown(
10
  '''
11
- # LangChain Test
12
- LLM跑步上车。
13
- LangChain进度:
14
-     2023/4/15,接入openAI,简单测试。
15
  ''')
16
  gr.Interface(fn=chatOpenAI, inputs="text", outputs="text")
17
  demo.launch()
 
6
  return llm(input)
7
 
8
  with gr.Blocks() as demo:
9
+ gr.Markdown("# LangChain Test,LLM跑步上车。")
10
  gr.Markdown(
11
  '''
12
+ | 日期 |   进度 | 备注 |
13
+ | :--: | :--- | :--: |
14
+ |   2023/04/14   |   接入openAI,简单测试。| |
15
+ | 2023/04/15 | &nbsp;&nbsp;熟悉LangChain API。<br> &nbsp;&nbsp;熟悉Chain的概念。 <br> &nbsp;&nbsp;熟悉agent goole search。<br> &nbsp;&nbsp;熟悉Prompt Template。| |
16
  ''')
17
  gr.Interface(fn=chatOpenAI, inputs="text", outputs="text")
18
  demo.launch()
openai_chat ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.chat_models import ChatOpenAI
2
+ from langchain.schema import (
3
+ AIMessage,
4
+ HumanMessage,
5
+ SystemMessage
6
+ )
7
+
8
+ chat = ChatOpenAI(temperature=0.5)
9
+ print(
10
+ chat([
11
+ SystemMessage(content="你是一名优秀的数学老师"),
12
+ HumanMessage(content="圆的面积怎么计算?"),
13
+ ]).content)
openai_chat_agent.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.agents import load_tools
2
+ from langchain.agents import initialize_agent
3
+ from langchain.agents import AgentType
4
+ from langchain.chat_models import ChatOpenAI
5
+ from langchain.llms import OpenAI
6
+
7
+ # First, let's load the language model we're going to use to control the agent.
8
+ chat = ChatOpenAI(temperature=0)
9
+
10
+ # Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.
11
+ llm = OpenAI(temperature=0)
12
+ tools = load_tools(["serpapi", "llm-math"], llm=llm)
13
+
14
+
15
+ # Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
16
+ agent = initialize_agent(tools, chat, agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
17
+
18
+ # Now let's test it out!
19
+ agent.run("作为一名优秀的投资理财市,您结合2023年100名优秀专业投资师的报告,给出投资建议。")
openai_chat_prompt_template.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.chat_models import ChatOpenAI
2
+ from langchain import LLMChain
3
+ from langchain.prompts.chat import (
4
+ ChatPromptTemplate,
5
+ SystemMessagePromptTemplate,
6
+ HumanMessagePromptTemplate
7
+ )
8
+
9
+ chat = ChatOpenAI(temperature=0)
10
+
11
+ template = "你是一名翻译助手。把{input_language} 翻译为 {output_language}。"
12
+ system_message_prompt = SystemMessagePromptTemplate.from_template(template)
13
+
14
+ human_template = "{text}"
15
+ human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
16
+
17
+ # 这里是使用chat请求,返回BaseMessage。
18
+ chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
19
+ result = chat(chat_prompt.format_prompt(input_language="中文", output_language="英语", text="我想请假").to_messages())
20
+ print(result.content)
21
+
22
+ # 这里是使用chain请求,返回str, 带有聊天模型的chain。
23
+ chain = LLMChain(llm=chat, prompt=chat_prompt)
24
+ result = chain.run(input_language="中文", output_language="英语", text="我想请假")
25
+ print(result)
openai_prompt_template.py CHANGED
@@ -7,5 +7,5 @@ prompt = PromptTemplate(
7
  input_variables=["product"],
8
  template="What is a good name for a company that makes {product}?"
9
  )
10
- chain = LLMChain(llm=llm,prompt=prompt)
11
  print(chain.run("colorful socks"))
 
7
  input_variables=["product"],
8
  template="What is a good name for a company that makes {product}?"
9
  )
10
+ chain = LLMChain(llm=llm, prompt=prompt)
11
  print(chain.run("colorful socks"))