Skip to content

Commit

Permalink
add section 2
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchen committed Jun 18, 2024
1 parent ef6f140 commit b412395
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 73 deletions.
50 changes: 50 additions & 0 deletions app/section_2_customer_runnable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import List


class CusotmerPiplineComponent:
steps: List["CustomerRunnable"]

def __init__(self, *steps):
self.steps = steps

def __or__(self, other: "CusotmerPiplineComponent"):
return CusotmerPiplineComponent(
*self.steps, other
)

def __ror__(self, other: "CusotmerPiplineComponent"):
print(other)

def __str__(self):
return ",".join(map(lambda item: item.name, self.steps))

def invoke(self,**kwargs):
print("kwargs:", kwargs)
for item in self.steps:
print(item.name)


class CustomerRunnable:
name: str

def __init__(self, name: str):
self.name = name

def __or__(self, other: "CustomerRunnable"):
# print(self.name,",",other.name)
return CusotmerPiplineComponent(
self, other
)

def __ror__(self, other: "CustomerRunnable"):
if not other:
return
# print(self.name,"+",other.name)

def __str__(self):
return self.name


if __name__ == '__main__':
res = CustomerRunnable("a") | CustomerRunnable("b") | CustomerRunnable("c")
res.invoke(**{"input":12})
122 changes: 70 additions & 52 deletions content/section_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T16:35:35.528615Z",
"start_time": "2024-06-13T16:35:35.050369Z"
"end_time": "2024-06-18T13:28:22.199803Z",
"start_time": "2024-06-18T13:28:21.515717Z"
}
},
"cell_type": "code",
Expand All @@ -56,8 +56,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T16:35:46.518080Z",
"start_time": "2024-06-13T16:35:42.832893Z"
"end_time": "2024-06-18T13:28:40.308087Z",
"start_time": "2024-06-18T13:28:22.201318Z"
}
},
"cell_type": "code",
Expand All @@ -67,7 +67,7 @@
{
"data": {
"text/plain": [
"AIMessage(content='你好,我是OpenAI的人工智能助手。我可以帮你回答问题、提供信息、并帮助你完成各种任务。', response_metadata={'token_usage': {'completion_tokens': 43, 'prompt_tokens': 13, 'total_tokens': 56}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-48d47b95-f12d-4593-833a-077fd52fcf3c-0', usage_metadata={'input_tokens': 13, 'output_tokens': 43, 'total_tokens': 56})"
"AIMessage(content='你好!我是ChatGPT,一个由OpenAI训练的大型语言模型。我可以帮助解答问题、提供信息、聊天等等。有什么我可以帮你的吗?', response_metadata={'token_usage': {'completion_tokens': 59, 'prompt_tokens': 13, 'total_tokens': 72}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-ab503481-2ac9-4e1c-ab9e-d02e2ef89e2c-0', usage_metadata={'input_tokens': 13, 'output_tokens': 59, 'total_tokens': 72})"
]
},
"execution_count": 2,
Expand All @@ -89,8 +89,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:19:52.217780Z",
"start_time": "2024-06-13T15:19:45.010014Z"
"end_time": "2024-06-18T13:28:50.368283Z",
"start_time": "2024-06-18T13:28:40.309155Z"
}
},
"cell_type": "code",
Expand Down Expand Up @@ -119,11 +119,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"content='看他们如何为你闪耀\\n还有你所做的一切\\n是的,他们都是黄色的\\n我过来了\\n我为你写了一首歌\\n还有你做的所有事情\\n那首歌叫做“黄色”\\n然后轮到我了\\n哦,做这件事情真是太棒了' response_metadata={'token_usage': {'completion_tokens': 98, 'prompt_tokens': 87, 'total_tokens': 185}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-88b99115-3b2c-405b-abc5-27aa8899685c-0' usage_metadata={'input_tokens': 87, 'output_tokens': 98, 'total_tokens': 185}\n"
"content='看它们如何为你闪耀\\n以及你所做的一切\\n是的,它们全都是黄色的\\n我走了过来\\n我为你写了一首歌\\n以及你所做的一切\\n这首歌叫做“黄色”\\n然后轮到我了\\n哦,这是多么了不起的事情' response_metadata={'token_usage': {'completion_tokens': 100, 'prompt_tokens': 87, 'total_tokens': 187}, 'model_name': 'gpt-4', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-90c06452-82db-404e-940a-6d720243c613-0' usage_metadata={'input_tokens': 87, 'output_tokens': 100, 'total_tokens': 187}\n"
]
}
],
"execution_count": 9
"execution_count": 3
},
{
"metadata": {},
Expand All @@ -138,8 +138,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:19:52.235707Z",
"start_time": "2024-06-13T15:19:52.231152Z"
"end_time": "2024-06-18T13:28:50.382258Z",
"start_time": "2024-06-18T13:28:50.369448Z"
}
},
"cell_type": "code",
Expand All @@ -154,15 +154,15 @@
{
"data": {
"text/plain": [
"'看他们如何为你闪耀\\n还有你所做的一切\\n是的,他们都是黄色的\\n我过来了\\n我为你写了一首歌\\n还有你做的所有事情\\n那首歌叫做“黄色”\\n然后轮到我了\\n哦,做这件事情真是太棒了'"
"'看它们如何为你闪耀\\n以及你所做的一切\\n是的,它们全都是黄色的\\n我走了过来\\n我为你写了一首歌\\n以及你所做的一切\\n这首歌叫做“黄色”\\n然后轮到我了\\n哦,这是多么了不起的事情'"
]
},
"execution_count": 10,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 10
"execution_count": 4
},
{
"metadata": {},
Expand All @@ -176,8 +176,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:21:29.712149Z",
"start_time": "2024-06-13T15:21:13.466240Z"
"end_time": "2024-06-18T13:28:57.548445Z",
"start_time": "2024-06-18T13:28:50.387233Z"
}
},
"cell_type": "code",
Expand All @@ -190,15 +190,15 @@
{
"data": {
"text/plain": [
"'看他们如何为你闪耀\\n和你所做的一切\\n是的,他们都是黄色的\\n我走过来\\n我为你写了一首歌\\n和你所做的一切\\n这就是名为“黄色”的那首歌\\n然后我开始轮到我\\n哦,做了这样的事情真是太了不起了'"
"'看它们如何为你闪耀\\n和你所做的一切\\n是的,它们全都是黄色的\\n我来了\\n我为你写了一首歌\\n和你所做的一切\\n它被称为“黄色”\\n于是我轮到我了\\n哦,做这件事真是太棒了'"
]
},
"execution_count": 13,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 13
"execution_count": 5
},
{
"metadata": {},
Expand Down Expand Up @@ -229,8 +229,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:38:19.189956Z",
"start_time": "2024-06-13T15:38:19.185742Z"
"end_time": "2024-06-18T13:28:57.557293Z",
"start_time": "2024-06-18T13:28:57.550225Z"
}
},
"cell_type": "code",
Expand All @@ -246,7 +246,7 @@
],
"id": "cda983a976bd0b4a",
"outputs": [],
"execution_count": 15
"execution_count": 6
},
{
"metadata": {},
Expand All @@ -257,8 +257,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:40:55.015489Z",
"start_time": "2024-06-13T15:40:55.011524Z"
"end_time": "2024-06-18T13:28:57.569174Z",
"start_time": "2024-06-18T13:28:57.559177Z"
}
},
"cell_type": "code",
Expand Down Expand Up @@ -292,12 +292,12 @@
"langchain_core.prompt_values.ChatPromptValue"
]
},
"execution_count": 33,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 33
"execution_count": 7
},
{
"metadata": {},
Expand All @@ -308,8 +308,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:40:38.185104Z",
"start_time": "2024-06-13T15:40:38.181207Z"
"end_time": "2024-06-18T13:28:57.572555Z",
"start_time": "2024-06-18T13:28:57.570110Z"
}
},
"cell_type": "code",
Expand All @@ -323,12 +323,12 @@
" HumanMessage(content=\"\\nLook how they shine for you\\nAnd everything you do\\nYeah' they were all Yellow\\nI came along\\nI wrote a song for you\\nAnd all the things you do\\nIt was called Yellow\\nSo then I took my turn\\nOh what a thing to have done\\n\")]"
]
},
"execution_count": 32,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 32
"execution_count": 8
},
{
"metadata": {},
Expand All @@ -339,8 +339,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:46:50.356501Z",
"start_time": "2024-06-13T15:46:39.338836Z"
"end_time": "2024-06-18T13:29:08.233368Z",
"start_time": "2024-06-18T13:28:57.573238Z"
}
},
"cell_type": "code",
Expand All @@ -366,18 +366,18 @@
"output_type": "stream",
"text": [
"너를 위해 어떻게 빛나는지 봐\n",
"그리고 네가 하는 모든 일에\n",
"그리고 네가 하는 모든 것들\n",
"그래, 그들은 모두 노란색이었어\n",
"나는 왔어\n",
"나는 너를 위한 노래를 썼어\n",
"그리고 너가 하는 모든 일에 대해\n",
"그것은 '노란색'이라고 불렸어\n",
"그래서 차례를 기다렸어\n",
", 그런 일을 한 것이야\n"
"나는 이곳에 왔어\n",
"너를 위한 노래를 썼어\n",
"그리고 네가 하는 모든 것들\n",
"그것은 '노란색'이라는 제목이었어\n",
"그래서 나는 차례를 가졌어\n",
", 그런 일을 한 것이 참 대단한 일이었어\n"
]
}
],
"execution_count": 36
"execution_count": 9
},
{
"metadata": {},
Expand All @@ -388,8 +388,8 @@
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-13T15:47:32.751931Z",
"start_time": "2024-06-13T15:47:26.257325Z"
"end_time": "2024-06-18T13:29:17.785976Z",
"start_time": "2024-06-18T13:29:08.234077Z"
}
},
"cell_type": "code",
Expand All @@ -399,15 +399,15 @@
{
"data": {
"text/plain": [
"'看看我如何为你闪耀\\n还有你做的所有事情\\n是的,它们都是黄色的\\n我来了\\n我为你写了一首歌\\n关于你做的所有事情\\n它被称为“黄色”\\n所以我等待着轮到我\\n哦,我做过这样的事情'"
"'看看你是如何闪耀的\\n以及你所做的一切\\n是的,它们都是黄色的\\n我来到这里\\n为你写了一首歌\\n以及你所做的一切\\n它的标题是\"黄色\"\\n所以我轮到了\\n哦,做这样的事情真是太棒了'"
]
},
"execution_count": 38,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 38
"execution_count": 10
},
{
"metadata": {},
Expand All @@ -432,15 +432,20 @@
"id": "ccf6c60dae8e1709"
},
{
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-18T13:29:17.789707Z",
"start_time": "2024-06-18T13:29:17.787840Z"
}
},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# ! pip install \"langserve[all]\"\n",
"# 我已经在requirements.txt中已经安装过了,这里不需要了"
],
"id": "a1b1183955736351"
"id": "a1b1183955736351",
"outputs": [],
"execution_count": 11
},
{
"metadata": {},
Expand All @@ -449,7 +454,12 @@
"id": "b7e21d291f347755"
},
{
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2024-06-18T13:29:18.191364Z",
"start_time": "2024-06-18T13:29:17.790651Z"
}
},
"cell_type": "code",
"source": [
"import uvicorn\n",
Expand Down Expand Up @@ -496,8 +506,16 @@
"# uvicorn.run(app, host=\"localhost\", port=8000)"
],
"id": "ac3255e570257601",
"outputs": [],
"execution_count": null
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"这里的代码需要新建一个py文件来跑,因为jupyter-book里面不能再启动event loop了\n"
]
}
],
"execution_count": 12
},
{
"metadata": {},
Expand Down
Loading

0 comments on commit b412395

Please sign in to comment.