Skip to main content

🆕 Github

https://github.com/marketplace/models

tip

我们支持所有Github模型,只需在发送litellm请求时将model=github/<任何github上的模型>设置为前缀

API密钥

# 环境变量
os.environ['GITHUB_API_KEY']

示例用法

from litellm import completion
import os

os.environ['GITHUB_API_KEY'] = ""
response = completion(
model="github/llama3-8b-8192",
messages=[
{"role": "user", "content": "hello from litellm"}
],
)
print(response)

示例用法 - 流式传输

from litellm import completion
import os

os.environ['GITHUB_API_KEY'] = ""
response = completion(
model="github/llama3-8b-8192",
messages=[
{"role": "user", "content": "hello from litellm"}
],
stream=True
)

for chunk in response:
print(chunk)

与LiteLLM代理一起使用

1. 在config.yaml中设置Github模型

model_list:
- model_name: github-llama3-8b-8192 # 用于请求的模型别名
litellm_params:
model: github/llama3-8b-8192
api_key: "os.environ/GITHUB_API_KEY" # 确保在.env中设置了`GITHUB_API_KEY`

2. 启动代理

litellm --config config.yaml

3. 测试它

向litellm代理发起请求

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
"model": "github-llama3-8b-8192",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
]
}
'
import openai
client = openai.OpenAI(
api_key="anything",
base_url="http://0.0.0.0:4000"
)

response = client.chat.completions.create(model="github-llama3-8b-8192", messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
])

print(response)

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage

chat = ChatOpenAI(
openai_api_base="http://0.0.0.0:4000", # 将openai_api_base设置为LiteLLM代理
model = "github-llama3-8b-8192",
temperature=0.1
)

messages = [
SystemMessage(
content="You are a helpful assistant that im using to make a test request to."
),
HumanMessage(
content="test from litellm. tell me why it's amazing in 1 sentence"
),
]
response = chat(messages)

print(response)

支持的模型 - 所有Github模型都支持!

我们支持所有Github模型,只需在发送完成请求时将github/设置为前缀

模型名称用法
llama-3.1-8b-instantcompletion(model="github/llama-3.1-8b-instant", messages)
llama-3.1-70b-versatilecompletion(model="github/llama-3.1-70b-versatile", messages)
llama3-8b-8192completion(model="github/llama3-8b-8192", messages)
llama3-70b-8192completion(model="github/llama3-70b-8192", messages)
llama2-70b-4096completion(model="github/llama2-70b-4096", messages)
mixtral-8x7b-32768completion(model="github/mixtral-8x7b-32768", messages)
gemma-7b-itcompletion(model="github/gemma-7b-it", messages)

Github - 工具/函数调用示例

# 示例伪函数,硬编码返回当前天气
import json
def get_current_weather(location, unit="fahrenheit"):
"""获取指定地点的当前天气"""
if "tokyo" in location.lower():
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": "celsius"})
elif "san francisco" in location.lower():
return json.dumps(
{"location": "San Francisco", "temperature": "72", "unit": "fahrenheit"}
)
elif "paris" in location.lower():
return json.dumps({"location": "Paris", "temperature": "22", "unit": "celsius"})
else:
return json.dumps({"location": location, "temperature": "unknown"})




# 步骤1:将对话和可用函数发送给模型
messages = [
{
"role": "system",
"content": "你是一个函数调用的LLM,使用从get_current_weather提取的数据回答关于旧金山天气的问题。",
},
{
"role": "user",
"content": "旧金山的天气怎么样?",
},
]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "获取指定地点的当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市和州,例如 San Francisco, CA",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
},
},
"required": ["location"],
},
},
}
]
response = litellm.completion(
model="github/llama3-8b-8192",
messages=messages,
tools=tools,
tool_choice="auto", # auto 是默认选项,但我们明确指定
)
print("Response\n", response)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls


# 步骤2:检查模型是否想要调用函数
if tool_calls:
# 步骤3:调用函数
# 注意:JSON响应可能不总是有效的;务必处理错误
available_functions = {
"get_current_weather": get_current_weather,
}
messages.append(
response_message
) # 将助手的回复扩展到对话中
print("Response message\n", response_message)
# 步骤4:将每个函数调用的信息和函数响应发送给模型
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(
location=function_args.get("location"),
unit=function_args.get("unit"),
)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
) # 将函数响应扩展到对话中
print(f"messages: {messages}")
second_response = litellm.completion(
model="github/llama3-8b-8192", messages=messages
) # 获取模型的新响应,模型可以看到函数响应
print("second response\n", second_response)

科技翻译指南

介绍

科技翻译需要高度的专业性和准确性。本指南旨在帮助译者理解科技文档的翻译要点,确保信息在不同语言之间无缝传达。

主要内容

  1. 术语一致性

    • 确保同一术语在文档中保持一致。
    • 使用术语表来统一专业词汇。
  2. 技术准确性

    • 理解并准确翻译技术细节。
    • 避免因文化差异导致的误解。
  3. 格式保留

    • 保持原文的格式,如标题、编号、表格等。
    • 确保翻译后的文档结构清晰。
  4. 文化适应

    • 适当调整内容以适应目标语言的文化背景。
    • 注意避免因文化差异导致的误解。

实践建议

  • 使用工具:利用CAT工具(如Trados)来提高效率和一致性。
  • 持续学习:关注科技领域的最新发展,更新知识库。
  • 反馈机制:建立反馈机制,及时纠正错误,提升翻译质量。

结论

科技翻译是一项挑战,但通过遵循本指南,译者可以有效提升翻译质量,确保信息的准确传达。

优云智算