diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000000000000000000000000000000000000..1c10e0efcc6a3e286a37391a07f85fd2208b228a --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$PROJECT_DIR$/00-chartgpt/chainlit/.chainlit/.langchain.db + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/00-chartgpt/chainlit/app.py b/00-chartgpt/chainlit/app.py new file mode 100644 index 0000000000000000000000000000000000000000..9948dceda04fd03110e8b60a8f1fcf055cf4d75a --- /dev/null +++ b/00-chartgpt/chainlit/app.py @@ -0,0 +1,20 @@ +from langchain import PromptTemplate, LLMChain +from langchain.chat_models import ChatOpenAI +import chainlit as cl +import os + +# os.environ["OPENAI_API_KEY"] = '70846324f01c4e5cb3cc11da28a1e091' +# os.environ["OPENAI_API_KEY"] = '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' +# os.environ["OPENAI_API_KEY"] = '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' +# os.environ["OPENAI_API_KEY"] = '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' +os.environ["OPENAI_API_KEY"] = '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' + +template = """Question: {question} +Answer: Let's think step by step.""" + + +@cl.langchain_factory(use_async=False) +def factory(): + prompt = PromptTemplate(template=template, input_variables=["question"]) + llm_chain = LLMChain(prompt=prompt, llm=ChatOpenAI(temperature=0, streaming=True)) + return llm_chain diff --git a/00-chartgpt/chainlit/azure_demo.py b/00-chartgpt/chainlit/azure_demo.py new file mode 100644 index 0000000000000000000000000000000000000000..e58ef1c3209734a5c33b13dd9a10b0b32061e843 --- /dev/null +++ b/00-chartgpt/chainlit/azure_demo.py @@ -0,0 +1,35 @@ +import os +import chainlit as cl + +from langchain.chat_models import ChatOpenAI +from langchain.schema import ( + HumanMessage, + SystemMessage +) + +# 公司的key +os.environ["OPENAI_API_KEY"] = '70846324f01c4e5cb3cc11da28a1e091' +os.environ["OPENAI_API_BASE"] = 'https://opencatgpt.openai.azure.com/' +os.environ["OPENAI_API_TYPE"] = 'azure' +os.environ["OPENAI_API_VERSION"] = '2023-05-15' + +chat = ChatOpenAI(model_name="gpt-35-turbo", engine="gpt-35-turbo") +history = [SystemMessage(content="你是一个聊天机器人,请回答下列问题。\n")] + + +@cl.on_message # this function will be called every time a user inputs a message in the UI +async def main(message: str): + # history = [SystemMessage(content="你是一个聊天机器人,请回答下列问题。\n")] + history.append(HumanMessage(content=message)) + res = await cl.make_async(sync_func)() + # res = chat(history) + print(res.content) + # this is an intermediate step + # await cl.Message(author="Tool 1", content=f"Response from tool1", indent=1).send() + # send back the final answer + history.append(res) + await cl.Message(content=f"{res.content}").send() + + +def sync_func(): + return chat(history) diff --git a/00-chartgpt/chainlit/chainlit_test_01.py b/00-chartgpt/chainlit/chainlit_test_01.py new file mode 100644 index 0000000000000000000000000000000000000000..f323cc5afc50034c871d33f43c548369dd5cc7a8 --- /dev/null +++ b/00-chartgpt/chainlit/chainlit_test_01.py @@ -0,0 +1,20 @@ +import chainlit as cl + + +@cl.on_chat_start +async def start(): + file = None + + # Wait for the user to upload a file + while files == None: + files = await cl.AskFileMessage( + content="Please upload a text file to begin!", accept=["text/plain"] + ).send() + # Decode the file + text_file = files[0] + text = text_file.content.decode("utf-8") + + # Let the user know that the system is ready + await cl.Message( + content=f"`{text_file.name}` uploaded, it contains {len(text)} characters!" + ).send() diff --git a/00-chartgpt/chainlit/chainlit_test_02.py b/00-chartgpt/chainlit/chainlit_test_02.py new file mode 100644 index 0000000000000000000000000000000000000000..db580e40e5cba3754ea7f195343489895a5aad0f --- /dev/null +++ b/00-chartgpt/chainlit/chainlit_test_02.py @@ -0,0 +1,9 @@ +import chainlit as cl + + +async def some_function(): + file = await cl.AskFileMessage( + content="Please upload a python file to begin!", accept={"text/plain": [".py"]} + ).send() + + diff --git a/00-chartgpt/chainlit/demo.py b/00-chartgpt/chainlit/demo.py new file mode 100644 index 0000000000000000000000000000000000000000..422e744d6d59a63e2edb98d3183777881da3f2ca --- /dev/null +++ b/00-chartgpt/chainlit/demo.py @@ -0,0 +1,12 @@ +import chainlit as cl + + +@cl.on_message # this function will be called every time a user inputs a message in the UI +async def main(message: str): + + + # this is an intermediate step + await cl.Message(author="Tool 1", content=f"Response from tool1", indent=1).send() + + # send back the final answer + await cl.Message(content=f"This is the final answer").send() diff --git a/00-chartgpt/chainlit/test_01.py b/00-chartgpt/chainlit/test_01.py new file mode 100644 index 0000000000000000000000000000000000000000..5c023610edf12845e57464f8483692f936b040e0 --- /dev/null +++ b/00-chartgpt/chainlit/test_01.py @@ -0,0 +1,18 @@ +import os +from langchain import PromptTemplate, OpenAI, LLMChain +import chainlit as cl + +# os.environ["OPENAI_API_KEY"] = "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" +os.environ["OPENAI_API_KEY"] = "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" + +template = """Question: {question} + +Answer: Let's think step by step.""" + + +@cl.langchain_factory(use_async=True) +def factory(): + prompt = PromptTemplate(template=template, input_variables=["question"]) + llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0), verbose=True) + + return llm_chain diff --git a/00-chartgpt/chainlit/test_app.py b/00-chartgpt/chainlit/test_app.py new file mode 100644 index 0000000000000000000000000000000000000000..4fea46c05946354faf4876db695819984355c7ea --- /dev/null +++ b/00-chartgpt/chainlit/test_app.py @@ -0,0 +1,38 @@ +import openai +import chainlit as cl + +openai.api_key = "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" +# model_name = "text-davinci-003" +model_name = "gpt-3.5-turbo" +settings = { + "temperature": 0.7, + "max_tokens": 500, + "top_p": 1, + "frequency_penalty": 0, + "presence_penalty": 0, +} + + +@cl.on_chat_start +def start_chat(): + cl.user_session.set( + "message_history", + [{"role": "system", "content": "You are a helpful assistant."}], + ) + + +@cl.on_message +async def main(message: str): + message_history = cl.user_session.get("message_history") + message_history.append({"role": "user", "content": message}) + + msg = cl.Message(content="") + + async for stream_resp in await openai.ChatCompletion.acreate( + model=model_name, messages=message_history, stream=True, **settings + ): + token = stream_resp.choices[0]["delta"].get("content", "") + await msg.stream_token(token) + + message_history.append({"role": "assistant", "content": msg.content}) + await msg.send() diff --git a/00-chartgpt/chainlit/test_app_01.py b/00-chartgpt/chainlit/test_app_01.py new file mode 100644 index 0000000000000000000000000000000000000000..327e21bad599e381353eafdeb43eca8ce8d3ec26 --- /dev/null +++ b/00-chartgpt/chainlit/test_app_01.py @@ -0,0 +1,32 @@ +import openai +import chainlit as cl + +openai.api_key = "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" +model_name = "text-davinci-003" +settings = { + "temperature": 0.7, + "max_tokens": 500, + "top_p": 1, + "frequency_penalty": 0, + "presence_penalty": 0, + "stop": ["```"], +} +prompt = """Answer the following question: +{question} +""" + + +@cl.on_message +async def main(message: str): + fromatted_prompt = prompt.format(question=message) + msg = cl.Message( + content="", + prompt=fromatted_prompt, + llm_settings=cl.LLMSettings(model_name=model_name, **settings), + ) + async for stream_resp in await openai.Completion.acreate( + model=model_name, prompt=fromatted_prompt, stream=True, **settings + ): + token = stream_resp.get("choices")[0].get("text") + await msg.stream_token(token) + await msg.send() diff --git a/00-chartgpt/http_01.py b/00-chartgpt/http_01.py new file mode 100644 index 0000000000000000000000000000000000000000..df32b8bded0f4f27290c204ae46abfe1530b0e73 --- /dev/null +++ b/00-chartgpt/http_01.py @@ -0,0 +1,6 @@ +# 使用urllib发送GET请求的示例代码如下: +import urllib.request + +response = urllib.request.urlopen('https://www.baidu.com/') +html = response.read() +print(html) diff --git a/00-chartgpt/http_02.py b/00-chartgpt/http_02.py new file mode 100644 index 0000000000000000000000000000000000000000..1c384956c46bf4eb0874e486b282d10371b6b17c --- /dev/null +++ b/00-chartgpt/http_02.py @@ -0,0 +1,5 @@ +# 使用requests发送GET请求的示例代码如下: +import requests + +response = requests.get('https://www.baidu.com/') +print(response.text) diff --git a/00-chartgpt/http_03.py b/00-chartgpt/http_03.py new file mode 100644 index 0000000000000000000000000000000000000000..91b99ae5758c07285eac50dc9e6ae322864edb3e --- /dev/null +++ b/00-chartgpt/http_03.py @@ -0,0 +1,11 @@ +# 对于POST请求,您可以通过向请求函数传递data参数来发送数据,如下所示: +import urllib.request +import urllib.parse + +url = 'https://www.baidu.com/' +values = {'name': 'John', 'age': 25} +data = urllib.parse.urlencode(values) +data = data.encode('ascii') # data应该是bytes类型 +req = urllib.request.Request(url, data) +response = urllib.request.urlopen(req) +print(response.read()) diff --git a/00-chartgpt/http_04.py b/00-chartgpt/http_04.py new file mode 100644 index 0000000000000000000000000000000000000000..0c493df7800788ac3949ab7eae35808a30b7591f --- /dev/null +++ b/00-chartgpt/http_04.py @@ -0,0 +1,7 @@ +# 使用requests发送POST请求的示例代码如下: +import requests + +url = 'https://www.baidu.com/' +values = {'name': 'John', 'age': 25} +response = requests.post(url, data=values) +print(response.text) diff --git "a/14_\345\210\267\351\242\230/problem_solving_01.py" "b/14_\345\210\267\351\242\230/problem_solving_01.py" index 6c363483c3e8447fa9b0499cb13f7936f4222f68..b66db93c7bfd0c169814516b575aeaa9e6203eae 100644 --- "a/14_\345\210\267\351\242\230/problem_solving_01.py" +++ "b/14_\345\210\267\351\242\230/problem_solving_01.py" @@ -1,5 +1,10 @@ +""" +01.两数之和 +""" + from typing import List + class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: diff --git "a/14_\345\210\267\351\242\230/problem_solving_02.py" "b/14_\345\210\267\351\242\230/problem_solving_02.py" new file mode 100644 index 0000000000000000000000000000000000000000..3fec830920136d27a04bbe2a849fe00a9c22799e --- /dev/null +++ "b/14_\345\210\267\351\242\230/problem_solving_02.py" @@ -0,0 +1,61 @@ +""" +26. 删除有序数组中的重复项 +给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。 + +考虑 nums 的唯一元素的数量为 k ,你需要做以下事情确保你的题解可以被通过: + +更改数组 nums ,使 nums 的前 k 个元素包含唯一元素,并按照它们最初在 nums 中出现的顺序排列。nums 的其余元素与 nums 的大小不重要。 +返回 k 。 +判题标准: + +系统会用下面的代码来测试你的题解: + +int[] nums = [...]; // 输入数组 +int[] expectedNums = [...]; // 长度正确的期望答案 + +int k = removeDuplicates(nums); // 调用 + +assert k == expectedNums.length; +for (int i = 0; i < k; i++) { + assert nums[i] == expectedNums[i]; +} +如果所有断言都通过,那么您的题解将被 通过。 + + + +示例 1: + +输入:nums = [1,1,2] +输出:2, nums = [1,2,_] +解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。 +示例 2: + +输入:nums = [0,0,1,1,1,2,2,3,3,4] +输出:5, nums = [0,1,2,3,4] +解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。 + + +提示: + +1 <= nums.length <= 3 * 104 +-104 <= nums[i] <= 104 +nums 已按 升序 排列 +""" +from typing import List + + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + """ + 列表去重后列表的长度 + :param nums: + :return: + """ + result = set(nums) + return len(result) + + +if __name__ == '__main__': + s = Solution() + li = s.removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]) + print(li) diff --git a/main.py b/main.py deleted file mode 100644 index 94e3a87232a22efe5c5534e3292a4a7efbaa6381..0000000000000000000000000000000000000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# This is a sample Python script. - -# Press ⌃R to execute it or replace it with your code. -# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint. - - -# Press the green button in the gutter to run the script. -if __name__ == '__main__': - print_hi('PyCharm') - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/