检索器#

Kork 代码链接受检索器接口,用于示例和外部函数。

内置检索器按原样返回它们初始化的内容。

您可以实现依赖于向量存储的动态检索器,并返回最相关的示例和外部函数,以完成手头的任务。

以下代码定义了聊天模型的占位符模型。您可以随意将其替换为真正的语言模型。

隐藏代码单元格源
from typing import Any, List, Optional

from langchain.chat_models.base import BaseChatModel
from langchain.schema import AIMessage, BaseMessage, ChatGeneration, ChatResult
from pydantic import Extra


class ToyChatModel(BaseChatModel):
    response: str

    class Config:
        """Configuration for this pydantic object."""

        extra = Extra.forbid
        arbitrary_types_allowed = True

    def _generate(
        self, messages: List[BaseMessage], stop: Optional[List[str]] = None
    ) -> ChatResult:
        message = AIMessage(content=self.response)
        generation = ChatGeneration(message=message)
        return ChatResult(generations=[generation])

    async def _agenerate(
        self, messages: List[BaseMessage], stop: Optional[List[str]] = None
    ) -> Any:
        """Async version of _generate."""
        message = AIMessage(content=self.response)
        generation = ChatGeneration(message=message)
        return ChatResult(generations=[generation])

上下文检索器#

上下文检索器(是的,这个名字不太好)支持一种方法,该方法接受用户 query 并返回外部函数列表。

内置检索器返回它初始化的信息,完全忽略 query

从抽象接口子类化以提供您自己的实现!

from kork import SimpleContextRetriever
import math
simple_context_retriever = SimpleContextRetriever.from_functions(
    [math.pow, math.log2, math.log10]
)
simple_context_retriever.retrieve("this is the query")
[ExternFunctionDef(name='pow', params=ParamList(params=[Param(name='x', type_='Any'), Param(name='y', type_='Any')]), return_type='Any', implementation=<built-in function pow>, doc_string='Return x**y (x to the power of y).'),
 ExternFunctionDef(name='log2', params=ParamList(params=[Param(name='x', type_='Any')]), return_type='Any', implementation=<built-in function log2>, doc_string='Return the base 2 logarithm of x.'),
 ExternFunctionDef(name='log10', params=ParamList(params=[Param(name='x', type_='Any')]), return_type='Any', implementation=<built-in function log10>, doc_string='Return the base 10 logarithm of x.')]

示例检索器#

支持与上下文检索器相同的 retrieve 方法。

内置示例检索器返回它初始化的信息,完全忽略 query

从抽象接口子类化以提供您自己的实现!

from kork import c_, r_, AstPrinter, SimpleExampleRetriever
language_name = "RollingMeow"
retriever = SimpleExampleRetriever.from_programs(
    language_name,
    [
        ("2**5", r_(c_(math.pow, 2, 5))),
        ("take the log base 2 of 2", r_(c_(math.log2, 2))),
    ],
    AstPrinter(),
)
retriever.retrieve("[ignore]")
[('2**5', '<code>var result = pow(2, 5)</code>'),
 ('take the log base 2 of 2', '<code>var result = log2(2)</code>')]

声明链#

from kork import (
    CodeChain,
    ast,
    AstPrinter,
)
chain = CodeChain.from_defaults(
    llm=ToyChatModel(response="MEOW MEOW MEOW MEOW"),  # The LLM to use
    ast_printer=AstPrinter(),  # Knows how to print the AST
    examples=retriever,  # Example programs
    context=simple_context_retriever,
    language_name=language_name,
)
_, few_shot_prompt = chain.prepare_context(query="hello")
print(few_shot_prompt.format_prompt(query="[user input]").to_string())
You are programming in a language called "RollingMeow".

You are an expert programmer and must follow the instructions below exactly.

Your goal is to translate a user query into a corresponding and valid RollingMeow
program.

You have access to the following external functions:

```RollingMeow
extern fn pow(x: Any, y: Any) -> Any // Return x**y (x to the power of y).
extern fn log2(x: Any) -> Any // Return the base 2 logarithm of x.
extern fn log10(x: Any) -> Any // Return the base 10 logarithm of x.
```


Do not assume that any other functions except for the ones listed above exist.

Wrap the program in <code> and </code> tags.

Store the solution to the query in a variable called "result".

Here is a sample valid program:

<code>
var x = 1 # Assign 1 to the variable x
var result = 1 + 2 # Calculate the sum of 1 + 2 and assign to result
var result = x # Assign the value of x to result
</code>

Guidelines:
- Do not use operators, instead invoke appropriate external functions.
- Do not declare functions, do not use loops, do not use conditionals.
- Solve the problem only using variable declarations and function invocations.

Begin!
Input: 2**5

Output: <code>var result = pow(2, 5)</code>

Input: take the log base 2 of 2

Output: <code>var result = log2(2)</code>

Input: [user input]

Output: