pip install langchain
pip install openai
export OPENAI_API_KEY="..."import os; os.environ["OPENAI_API_KEY"] = "..."text = "What would be a good company name a company that makes colorful socks?"
print(llm(text)) # 返回 Socktastic!
简单数学问题:
from langchain.llms import OpenAI # 导入 LLM wrapper
llm = OpenAI(temperature=0.9) # 大的 temperature 会让输出有更多的随机性
text = "what is the results of 5+6?"
print(llm(text)) # 返回 11
text = "what is the results of 55+66?"
print(llm(text)) # 返回 121
text = "what is the results of 55555+66666?"
print(llm(text)) # 返回 122221
text = "what is the results of 512311+89749878?"
print(llm(text)) # 返回 89,876,189,终于错了...
另一个例子,这里返回的是同义词,如果要返回同音词则需要修改输入的 prompt(另外一个解决方式是基于以下章节中的 Memory 模式):
text = "what word is similar to good?"
print(llm(text)) # 返回 Excellent
text = "what word is homophone of good?"
print(llm(text)) # 返回 Goo
from langchain.prompts import PromptTemplate
prompt = PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
print(prompt.format(product="colorful socks")) # 返回 What is a good name for a company that makes colorful socks?
text = prompt.format(product="colorful socks")
print(llm(text)) # 返回 Socktastic!
text = prompt.format(product="chocolates")
print(llm(text)) # 返回 ChocoDelightz!
该实现基于论文: MemPrompt

对话任务中的 ConversationChain 示例(ConversationBufferMemory 模式),verbose=True 会输出对话任务中的 prompt,可以看到之前聊天会作为短期 memory 加在 prompt 中,从而让模型能有短时间的记忆能力:
from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
conversation.predict(input="Hi there!") # 返回如下
#> Entering new ConversationChain chain...
#Prompt after formatting:
#The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific #details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
#Current conversation:
#Human: Hi there!
#AI:
#> Finished chain.
# Out[53]: " Hi there! It's nice to meet you. How can I help you today?"
conversation.predict(input="I'm doing well! Just having a conversation with an AI.") # 返回如下
#Prompt after formatting:
#The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific #details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
#Current conversation:
#Human: Hi there!
#AI: Hi there! It's nice to meet you. How can I help you today?
#Human: I'm doing well! Just having a conversation with an AI.
#AI:
#> Finished chain.
#Out[54]: " That's great! It's always nice to have a conversation with someone new. What would you like to talk about?"
conversation.predict(input="what word is similar to good?") # 返回 ' Synonyms for "good" include excellent, great, fine, and superb.'
conversation.predict(input="similar to means with similar pronunciation") # 返回 ' Ah, I see. Synonyms for "good" with similar pronunciation include wood, hood, and should.'
这里的实现看起来和 memprompt 非常类似,每个问题不会直接回答答案,而是回答 understating+answer,从而让用户可以基于对 understating 的理解来判断模型反馈是否符合用户的预期,而不用直接判断 answer 的正确性
对话任务中的其他几种 memory 添加模式
更高级的 memory 使用方式
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
我在app/helpers/sessions_helper.rb中有一个帮助程序文件,其中包含一个方法my_preference,它返回当前登录用户的首选项。我想在集成测试中访问该方法。例如,这样我就可以在测试中使用getuser_path(my_preference)。在其他帖子中,我读到这可以通过在测试文件中包含requiresessions_helper来实现,但我仍然收到错误NameError:undefinedlocalvariableormethod'my_preference'.我做错了什么?require'test_helper'require'sessions_hel
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案