MistralAI Codestral 烹饪手册¶
MistralAI 发布了 codestral-latest - 一个代码模型。
Codestral 是 MistralAI 新推出的代码模型,专为代码生成而设计,精通 80 多种编程语言。它通过完成函数、编写测试和填充代码片段来简化编码任务,提高开发人员效率并减少错误。Codestral 通过统一的 API 端点进行操作,使其成为软件开发的多功能工具。
本烹饪手册展示了如何使用 codestral-latest
模型与 llama-index。它指导您使用 Codestral 的“填充中间”(fill-in-the-middle) 和指令 (instruct) 端点。
设置 LLM¶
In [ ]
已复制!
import os
os.environ["MISTRAL_API_KEY"] = "<YOUR MISTRAL API KEY>"
from llama_index.llms.mistralai import MistralAI
llm = MistralAI(model="codestral-latest", temperature=0.1)
import os os.environ["MISTRAL_API_KEY"] = "" from llama_index.llms.mistralai import MistralAI llm = MistralAI(model="codestral-latest", temperature=0.1)
指令模式用法¶
编写一个斐波那契函数¶
In [ ]
已复制!
from llama_index.core.llms import ChatMessage
messages = [ChatMessage(role="user", content="Write a function for fibonacci")]
response = llm.chat(messages)
print(response)
from llama_index.core.llms import ChatMessage messages = [ChatMessage(role="user", content="Write a function for fibonacci")] response = llm.chat(messages) print(response)
assistant: Sure, here is a simple Python function that calculates the nth number in the Fibonacci sequence: ```python def fibonacci(n): if n <= 0: print("Input should be positive integer.") elif n == 1: return 0 elif n == 2: return 1 else: a, b = 0, 1 for i in range(2, n): a, b = b, a + b return b ``` You can use this function to find the nth number in the Fibonacci sequence by calling `fibonacci(n)`, where `n` is the position of the number you want to find. For example, `fibonacci(10)` will return the 10th number in the Fibonacci sequence.
使用 LlamaIndex 构建 RAG 流水线的函数¶
注意:输出大部分准确,但基于旧版本的 LlamaIndex 包。
In [ ]
已复制!
messages = [
ChatMessage(
role="user",
content="Write a function to build RAG pipeline using LlamaIndex.",
)
]
response = llm.chat(messages)
print(response)
messages = [ ChatMessage( role="user", content="Write a function to build RAG pipeline using LlamaIndex.", ) ] response = llm.chat(messages) print(response)
assistant: Sure, I can help you with that. Here's a basic example of how you can build a Retrieval Augmented Generation (RAG) pipeline using LlamaIndex. This example assumes that you have a list of documents. ```python from llama_index import VectorStoreIndex, SimpleDirectoryReader def build_rag_pipeline(documents_path): # Load documents documents = SimpleDirectoryReader(documents_path).load_data() # Create index index = VectorStoreIndex.from_documents(documents) # Create query engine query_engine = index.as_query_engine() return query_engine # Usage query_engine = build_rag_pipeline("path_to_your_documents") response = query_engine.query("Your query here") print(response) ``` In this code: 1. We first import the necessary classes from LlamaIndex. 2. We define a function `build_rag_pipeline` that takes a path to a directory of documents as input. 3. We load the documents using `SimpleDirectoryReader`. 4. We create an index from the documents using `VectorStoreIndex.from_documents`. 5. We create a query engine from the index using `index.as_query_engine`. 6. Finally, we return the query engine. You can use the query engine to ask questions about the documents. The query engine will use the index to retrieve relevant documents and then generate a response based on those documents.
填充中间¶
此功能允许用户设置一个带有提示的起始点,以及一个带有后缀和停止符的可选结束点。然后 Codestral 模型会生成中间的代码,非常适合需要特定代码生成的任务。
用代码的开始和结束填充代码¶
In [ ]
已复制!
prompt = "def multiply("
suffix = "return a*b"
response = llm.fill_in_middle(prompt, suffix)
print(
f"""
{prompt}
{response.text}
{suffix}
"""
)
prompt = "def multiply(" suffix = "return a*b" response = llm.fill_in_middle(prompt, suffix) print( f""" {prompt} {response.text} {suffix} """ )
def multiply( a, b): """ This function multiplies two numbers """ return a*b
用代码的开始、结束和停止 token 填充代码¶
In [ ]
已复制!
prompt = "def multiply(a,"
suffix = ""
stop = ["\n\n\n"]
response = llm.fill_in_middle(prompt, suffix, stop)
print(
f"""
{prompt}
{response.text}
{suffix}
"""
)
prompt = "def multiply(a," suffix = "" stop = ["\n\n\n"] response = llm.fill_in_middle(prompt, suffix, stop) print( f""" {prompt} {response.text} {suffix} """ )
def multiply(a, b): return a * b # test the function print(multiply(2, 3)) # should print 6 print(multiply(-1, 5)) # should print -5 print(multiply(0, 99)) # should print 0 # we can also test the function with large numbers print(multiply(123456789, 987654321)) # should print 121932631132635269 # the function should also work with floating point numbers print(multiply(3.14, 2.71)) # should print approximately 8.5392 # the function should also work with negative floating point numbers print(multiply(-3.14, 2.71)) # should print approximately -8.5392 # the function should also work with mixed types (integer and floating point) print(multiply(2, 3.14)) # should print approximately 6.28