你好美丽的人!
我目前正试图在OpenAI的帮助下编写自己的“AI”。我遵循Langchain并设法最终得到了以下代码:
import os
import re
import discord
import requests
from discord.ext import commands
from langchain.chains import ConversationalRetrievalChain
from langchain.embeddings import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from transformers import GPT2TokenizerFast
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
# Set up OpenAI API key and models
os.environ["OPENAI_API_KEY"] = 'xxxxxx'
def get_documentation():
zendesk_url = "https://test.zendesk.com/api/v2/help_center/articles.json"
documentation = []
while zendesk_url:
# Make a GET request to the Zendesk API to fetch articles for the current page
response = requests.get(
zendesk_url,
headers={
"Authorization": f"Basic xxxx",
"Content-Type": "application/json"
})
# Check if the request was successful
if response.status_code == 200:
response_json = response.json()
# Loop through the articles on the current page
for article in response_json["articles"]:
# Extract the title and body of the article
title = article['title']
body = article['body']
# Remove any HTML tags and formatting from the body
body = re.sub('<[^<]+?>', '', body)
# Remove all newline characters from the body
body = body.replace('\n', ' ')
# Replace non-breaking spaces with regular spaces
body = body.replace('\xa0', ' ')
# Append the title and body to the documentation list
documentation.append((title, body))
# Check if there are more pages of articles and update the zendesk_url variable if necessary
next_page_url = response_json["next_page"]
zendesk_url = next_page_url if next_page_url else None
else:
# If the request was not successful, raise an exception with the error message
response.raise_for_status()
return documentation
# Load the GPT2 tokenizer
tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
print(tokenizer)
# Define a function to count tokens
def count_tokens(text: str) -> int:
return len(tokenizer.encode(text))
# Create a text splitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=512,
chunk_overlap=24,
length_function=count_tokens,
)
# Fetch and clean the documentation
documentation = get_documentation() # The len of documentation is 93
# Extract only the article bodies
article_bodies = [article_body for title, article_body in documentation]
# Split the article bodies into chunks
chunks = text_splitter.create_documents(article_bodies)
# Get embedding model
embeddings = OpenAIEmbeddings()
# Create vector database
db = FAISS.from_documents(chunks, embeddings)
qa = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0.1), db.as_retriever())
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
chat_history = []
@bot.command()
async def ask(ctx, *, question):
print(f"{ctx.author.name} asked: {question}")
result = qa(
{
"question": question,
"chat_history": chat_history
}
)
chat_history.append((question, result['answer']))
await ctx.send(result['answer'])
bot.run('xxxxxx')
字符串
我所做的是连接到我的zendesk,通过调用get_documentation()来抓取所有文档,然后使用它进行块。当我打电话!在这里提问,我应该得到答案。查看我的最新使用情况。它最终使用了大量的令牌,我觉得它可能太多了,可能需要一些解释,或者如果有什么我甚至可以改进?
x1c 0d1x的数据
我知道当我开始写脚本时,通常会有大约46,179个提示,但我真的不明白为什么我甚至没有开始问问题就付钱了。我如何改进它以使用更少的令牌?
预期:
使用较少的令牌/在我询问提示时使用令牌
实际:
每次启动时使用40 k+令牌。
1条答案
按热度按时间y4ekin9u1#
从here开始:
令牌化是将输入和输出文本拆分为可以由LLM AI模型处理的较小单元的过程。标记可以是单词、字符、子单词或符号,具体取决于模型的类型和大小。令牌化可以帮助模型处理不同的语言、词汇表和格式,并降低计算和内存成本。令牌化还可以通过影响令牌的含义和上下文来影响生成的文本的质量和多样性。根据文本的复杂性和可变性,可以使用不同的方法来完成标记化,例如基于规则的,统计的或神经的。
令牌的使用基本上取决于输入和输出长度以及模型配置。甚至单个标点符号也可以被模型分类为记号。您可以在enter link description here测试令牌使用情况
的数据
在上面的示例中,“,”和“.”被视为一个标记。为了减少令牌使用
max_tokens
命名参数。较长的输出需要生成更多的令牌。当您使用max_tokens
参数设置输出长度限制时,模型将在达到该令牌限制时停止生成文本。gpt-3.5-turbo
是GPT-3
的令牌效率更高的版本。